From mboxrd@z Thu Jan 1 00:00:00 1970 From: Charles Bailey Subject: [PATCH 2/3] Move unsigned long option parsing out of pack-objects.c Date: Fri, 19 Jun 2015 10:10:58 +0100 Message-ID: <1434705059-2793-3-git-send-email-charles@hashpling.org> References: <1434705059-2793-1-git-send-email-charles@hashpling.org> To: Junio Hamano , git@vger.kernel.org X-From: git-owner@vger.kernel.org Fri Jun 19 11:37:34 2015 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Z5sjs-0004sp-OC for gcvg-git-2@plane.gmane.org; Fri, 19 Jun 2015 11:37:33 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754064AbbFSJha (ORCPT ); Fri, 19 Jun 2015 05:37:30 -0400 Received: from host02.zombieandprude.com ([80.82.119.138]:51098 "EHLO host02.zombieandprude.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753522AbbFSJh1 (ORCPT ); Fri, 19 Jun 2015 05:37:27 -0400 Received: from hashpling.plus.com ([212.159.69.125]:41965) by host02.zombieandprude.com with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA256:128) (Exim 4.80) (envelope-from ) id 1Z5sKQ-0006de-Eg; Fri, 19 Jun 2015 10:11:14 +0100 X-Mailer: git-send-email 2.4.0.53.g8440f74 In-Reply-To: <1434705059-2793-1-git-send-email-charles@hashpling.org> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: From: Charles Bailey The unsigned long option parsing (including 'k'/'m'/'g' suffix parsing) is more widely applicable. Add support for OPT_ULONG to parse-options.h and change pack-objects.c use this support. Signed-off-by: Charles Bailey --- builtin/pack-objects.c | 17 ----------------- parse-options.c | 15 +++++++++++++++ parse-options.h | 5 ++++- t/t0040-parse-options.sh | 46 ++++++++++++++++++++++++++++++++++++++++++---- test-parse-options.c | 3 +++ 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index 80fe8c7..5de76db 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -2588,23 +2588,6 @@ static int option_parse_unpack_unreachable(const struct option *opt, return 0; } -static int option_parse_ulong(const struct option *opt, - const char *arg, int unset) -{ - if (unset) - die(_("option %s does not accept negative form"), - opt->long_name); - - if (!git_parse_ulong(arg, opt->value)) - die(_("unable to parse value '%s' for option %s"), - arg, opt->long_name); - return 0; -} - -#define OPT_ULONG(s, l, v, h) \ - { OPTION_CALLBACK, (s), (l), (v), "n", (h), \ - PARSE_OPT_NONEG, option_parse_ulong } - int cmd_pack_objects(int argc, const char **argv, const char *prefix) { int use_internal_rev_list = 0; diff --git a/parse-options.c b/parse-options.c index 80106c0..76a5c3e 100644 --- a/parse-options.c +++ b/parse-options.c @@ -180,6 +180,21 @@ 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; + return 0; + } + if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { + *(unsigned long *)opt->value = opt->defval; + return 0; + } + if (get_arg(p, opt, flags, &arg)) + return -1; + if (!git_parse_ulong(arg, opt->value)) + return opterror(opt, "expects a numerical value", flags); + return 0; + default: die("should not happen, someone must be hit on the forehead"); } diff --git a/parse-options.h b/parse-options.h index c71e9da..2ddb26f 100644 --- a/parse-options.h +++ b/parse-options.h @@ -18,7 +18,8 @@ enum parse_opt_type { OPTION_INTEGER, OPTION_CALLBACK, OPTION_LOWLEVEL_CALLBACK, - OPTION_FILENAME + OPTION_FILENAME, + OPTION_ULONG }; enum parse_opt_flags { @@ -129,6 +130,8 @@ struct option { #define OPT_CMDMODE(s, l, v, h, i) { OPTION_CMDMODE, (s), (l), (v), NULL, \ (h), PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) } #define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), N_("n"), (h) } +#define OPT_ULONG(s, l, v, h) { OPTION_ULONG, (s), (l), (v), N_("n"), \ + (h), PARSE_OPT_NONEG } #define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) } #define OPT_STRING_LIST(s, l, v, a, h) \ { OPTION_CALLBACK, (s), (l), (v), (a), \ diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index ecb7417..55b3dba 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -19,6 +19,8 @@ usage: test-parse-options -i, --integer get a integer -j get a integer, too + -u, --unsigned-long + get an unsigned long --set23 set integer to 23 -t