From 9ca1169fd92c71ebbef92ff18aa5d91a2157d1bd Mon Sep 17 00:00:00 2001 From: Stephen Boyd Date: Sun, 5 Dec 2010 23:57:42 -0800 Subject: parse-options: Don't call parse_options_check() so much parse_options_check() is being called for each invocation of parse_options_step which can be quite a bit for some commands. The commit introducing this function cb9d398 (parse-options: add parse_options_check to validate option specs., 2009-06-09) had the correct motivation and explicitly states that parse_options_check() should be called from parse_options_start(). However, the implementation differs from the motivation. Fix it. Signed-off-by: Stephen Boyd Signed-off-by: Junio C Hamano --- parse-options.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'parse-options.h') diff --git a/parse-options.h b/parse-options.h index d982f0f1b..5eb499b99 100644 --- a/parse-options.h +++ b/parse-options.h @@ -180,7 +180,7 @@ struct parse_opt_ctx_t { extern void parse_options_start(struct parse_opt_ctx_t *ctx, int argc, const char **argv, const char *prefix, - int flags); + const struct option *options, int flags); extern int parse_options_step(struct parse_opt_ctx_t *ctx, const struct option *options, -- cgit v1.2.1 From b0b3a8b666ac9bcab93c9b05ca7de918d7fa18bc Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Wed, 1 Dec 2010 17:32:16 -0600 Subject: parse-options: allow git commands to invent new option types parse-options provides a variety of option behaviors, including OPTION_CALLBACK, which should take care of just about any sane behavior. All supported behaviors obey the following constraint: A --foo option can only accept (and base its behavior on) one argument, which would be the following command-line argument in the "unsticked" form. Alas, some existing git commands have options that do not obey that constraint. For example, update-index --cacheinfo takes three arguments, and update-index --resolve takes all later parameters as arguments. Introduces an OPTION_LOWLEVEL_CALLBACK backdoor to parse-options so such option types can be supported without tempting inventors of other commands through mention in the public API. Commands can set the callback field to a function accepting three arguments: the option parsing context, the option itself, and a flag indicating whether the the option was negated. When the option is encountered, that function is called to take over from get_value(). The return value should be zero for success, -1 for usage errors. Thanks to Stephen Boyd for API guidance. Improved-by: Stephen Boyd Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- parse-options.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'parse-options.h') diff --git a/parse-options.h b/parse-options.h index 5eb499b99..470bb3329 100644 --- a/parse-options.h +++ b/parse-options.h @@ -17,6 +17,7 @@ enum parse_opt_type { OPTION_STRING, OPTION_INTEGER, OPTION_CALLBACK, + OPTION_LOWLEVEL_CALLBACK, OPTION_FILENAME }; @@ -43,6 +44,10 @@ enum parse_opt_option_flags { struct option; typedef int parse_opt_cb(const struct option *, const char *arg, int unset); +struct parse_opt_ctx_t; +typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx, + const struct option *opt, int unset); + /* * `type`:: * holds the type of the option, you must have an OPTION_END last in your @@ -87,7 +92,8 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset); * useful for users of OPTION_NEGBIT. * * `callback`:: - * pointer to the callback to use for OPTION_CALLBACK. + * pointer to the callback to use for OPTION_CALLBACK or + * OPTION_LOWLEVEL_CALLBACK. * * `defval`:: * default value to fill (*->value) with for PARSE_OPT_OPTARG. -- cgit v1.2.1 From 979240fee32628c317998f3c3fe2619cf01decc2 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Wed, 1 Dec 2010 17:32:55 -0600 Subject: parse-options: make resuming easier after PARSE_OPT_STOP_AT_NON_OPTION Introduce a PARSE_OPT_NON_OPTION state, so parse_option_step() callers can easily distinguish between non-options and other reasons for option parsing termination (like "--"). Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- parse-options.h | 1 + 1 file changed, 1 insertion(+) (limited to 'parse-options.h') diff --git a/parse-options.h b/parse-options.h index 470bb3329..3c2ec1d09 100644 --- a/parse-options.h +++ b/parse-options.h @@ -167,6 +167,7 @@ extern NORETURN void usage_msg_opt(const char *msg, enum { PARSE_OPT_HELP = -1, PARSE_OPT_DONE, + PARSE_OPT_NON_OPTION, PARSE_OPT_UNKNOWN }; -- cgit v1.2.1