Duy Nguyen <pclo...@gmail.com> writes:

> The reason it's in parse_options_step() is because -h is also handled
> in there. Although -h does not bury exit() deep in the call chain. So
> how about this as a replacement?

So just like -h returns PARSE_OPT_HELP and causes the calling
parse_options() to exit(129), the new _COMPLETE can be used to
trigger an early & successful return?

We'd also have to cover builtin/{blame,shortlog,update-index}.c
where they switch() on the return value from parse_options_step(),
but other than that, it probably is a better approach.  The users of
parse_options_step() that avoid parse_options() want to handle the
various "we stopped by hitting a non-option" cases in their own
ways, so treating this special action the same way as -h is treated
would be sensible.  We _might_ want to standardize some of the case
arms in these custom switch statements that appear in these three
built-in command implementations, but that can be done more easily
if this step is done like you showed below, I think.





> -- 8< --
> diff --git a/parse-options.c b/parse-options.c
> index 3b874a83a0..6932eaff61 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -516,7 +516,7 @@ static int show_gitcomp(struct parse_opt_ctx_t *ctx,
>       show_negated_gitcomp(original_opts, -1);
>       show_negated_gitcomp(original_opts, nr_noopts);
>       fputc('\n', stdout);
> -     exit(0);
> +     return PARSE_OPT_COMPLETE;
>  }
>  
>  static int usage_with_options_internal(struct parse_opt_ctx_t *,
> @@ -638,6 +638,8 @@ int parse_options(int argc, const char **argv, const char 
> *prefix,
>       case PARSE_OPT_HELP:
>       case PARSE_OPT_ERROR:
>               exit(129);
> +     case PARSE_OPT_COMPLETE:
> +             exit(0);
>       case PARSE_OPT_NON_OPTION:
>       case PARSE_OPT_DONE:
>               break;
> diff --git a/parse-options.h b/parse-options.h
> index 6c4fe2016d..a650a7d220 100644
> --- a/parse-options.h
> +++ b/parse-options.h
> @@ -208,6 +208,7 @@ extern int opterror(const struct option *opt, const char 
> *reason, int flags);
>  /*----- incremental advanced APIs -----*/
>  
>  enum {
> +     PARSE_OPT_COMPLETE = -2,
>       PARSE_OPT_HELP = -1,
>       PARSE_OPT_DONE,
>       PARSE_OPT_NON_OPTION,
> -- 8< --

Reply via email to