diff options
| -rw-r--r-- | Documentation/technical/api-parse-options.adoc | 6 | ||||
| -rw-r--r-- | parse-options.c | 8 | ||||
| -rwxr-xr-x | t/t0040-parse-options.sh | 4 |
3 files changed, 11 insertions, 7 deletions
diff --git a/Documentation/technical/api-parse-options.adoc b/Documentation/technical/api-parse-options.adoc index 61fa6ee167..63acfb419b 100644 --- a/Documentation/technical/api-parse-options.adoc +++ b/Documentation/technical/api-parse-options.adoc @@ -211,8 +211,10 @@ There are some macros to easily define options: Use of `--no-option` will clear the list of preceding values. `OPT_INTEGER(short, long, &int_var, description)`:: - Introduce an option with integer argument. - The integer is put into `int_var`. + Introduce an option with integer argument. The argument must be a + integer and may include a suffix of 'k', 'm' or 'g' to + scale the provided value by 1024, 1024^2 or 1024^3 respectively. + The scaled value is put into `int_var`. `OPT_MAGNITUDE(short, long, &unsigned_long_var, description)`:: Introduce an option with a size argument. The argument must be a diff --git a/parse-options.c b/parse-options.c index 35fbb3b0d6..b287436e81 100644 --- a/parse-options.c +++ b/parse-options.c @@ -73,7 +73,7 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, enum opt_parsed flags, const char **argp) { - const char *s, *arg; + const char *arg; const int unset = flags & OPT_UNSET; int err; @@ -185,9 +185,9 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, if (!*arg) return error(_("%s expects a numerical value"), optname(opt, flags)); - *(int *)opt->value = strtol(arg, (char **)&s, 10); - if (*s) - return error(_("%s expects a numerical value"), + if (!git_parse_int(arg, opt->value)) + return error(_("%s expects an integer value" + " with an optional k/m/g suffix"), optname(opt, flags)); return 0; diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index 2fe3522305..0c538c4b43 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -111,7 +111,9 @@ test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt' -test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345' +test_expect_success 'OPT_INTEGER() negative' 'check integer: -2345 -i -2345' +test_expect_success 'OPT_INTEGER() kilo' 'check integer: 239616 -i 234k' +test_expect_success 'OPT_INTEGER() negative kilo' 'check integer: -239616 -i -234k' test_expect_success 'OPT_MAGNITUDE() simple' ' check magnitude: 2345678 -m 2345678 |
