From 81dd4fd384d24ad247a88d1d04778de241e1781a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 21 Jan 2014 15:29:58 -0800 Subject: parse-options: refactor human-friendly-integer parser out of pack-objects We only had code to understand unit suffixes such as g/m/k (as in 2g/400m/8k) in the configuration parser but not in the command line option parser. "git pack-objects" worked it around by having a custom extension to the parse-options API; make it available to other callers. The new macro is not called OPT_ULONG() but OPT_HUM_ULONG(), as it would be bizzarre to have ULONG that understands human friendly units while INTEGER that does not. It is not named with a shorter "OPT_HULONG", primarily to avoid having to name a future parallel for parsing human friendly integer "OPT_HINT". Signed-off-by: Junio C Hamano --- parse-options.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index c2cbca25cc..948ade7cea 100644 --- a/parse-options.c +++ b/parse-options.c @@ -136,6 +136,23 @@ static int get_value(struct parse_opt_ctx_t *p, return opterror(opt, "expects a numerical value", flags); return 0; + case OPTION_ULONG: + if (unset) { + *(unsigned long *)opt->value = 0; + } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { + *(unsigned long *)opt->value = opt->defval; + } else if (get_arg(p, opt, flags, &arg)) { + return -1; + } else if (opt->flags & PARSE_OPT_HUMAN_NUMBER) { + if (!git_parse_ulong(arg, (unsigned long *)opt->value)) + return opterror(opt, "expects a numerical value", flags); + } else { + *(unsigned long *)opt->value = strtoul(arg, (char **)&s, 10); + if (*s) + return opterror(opt, "expects a numerical value", flags); + } + return 0; + default: die("should not happen, someone must be hit on the forehead"); } -- cgit v1.2.1