Jeff King <p...@peff.net> writes:

> If we don't mind a one-time pain, I think we can just convert the
> existing usage() to something more like usage_with_options(). The patch
> below does that (and teaches the latter to handle a NULL options field).
>
> The diff is ugly, and the conversion is mostly mechanical. But I think
> some sites can be improved. For example, look at the change in bundle.c,
> which now hands a set of strings rather than formatting the whole "or:"
> chain manually.
> ...
> With bonus points for actually describing each option (though at that
> point it may be as much work to just convert the thing to parse-options,
> which would also be fine with me).
>
> That's tedious work which would need attention paid to each individual
> command. So I'd probably do a single mechanical patch like this one
> (that keeps the output identical), and then let people fix each one up
> on top.

Yup.  Unlike my other hack, this does look more useful to me.  It
was kind of surprising that the change to parse-options is just a
single liner, but it's just "no input resulting in no output" ;-)

> diff --git a/git.c b/git.c
> index 1b8b7f51a..3496f8a23 100644
> --- a/git.c
> +++ b/git.c
> @@ -3,12 +3,14 @@
>  #include "help.h"
>  #include "run-command.h"
>  
> -const char git_usage_string[] =
> +const char *git_usage_string[] = {
>       "git [--version] [--help] [-C <path>] [-c name=value]\n"
>       "           [--exec-path[=<path>]] [--html-path] [--man-path] 
> [--info-path]\n"
>       "           [-p | --paginate | --no-pager] [--no-replace-objects] 
> [--bare]\n"
>       "           [--git-dir=<path>] [--work-tree=<path>] 
> [--namespace=<name>]\n"
> -     "           <command> [<args>]";
> +     "           <command> [<args>]",
> +     NULL
> +};
>  
>  const char git_more_info_string[] =
>       N_("'git help -a' and 'git help -g' list available subcommands and 
> some\n"
> @@ -694,7 +696,7 @@ int cmd_main(int argc, const char **argv)
>       } else {
>               /* The user didn't specify a command; give them help */
>               commit_pager_choice();
> -             printf("usage: %s\n\n", git_usage_string);
> +             printf("usage: %s\n\n", git_usage_string[0]);
>               list_common_cmds_help();
>               printf("\n%s\n", _(git_more_info_string));
>               exit(1);


> diff --git a/parse-options.c b/parse-options.c
> index a23a1e67f..b0cc2a410 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -600,6 +600,9 @@ static int usage_with_options_internal(struct 
> parse_opt_ctx_t *ctx,
>               usagestr++;
>       }
>  
> +     if (!opts)
> +             return PARSE_OPT_HELP;
> +
>       if (opts->type != OPTION_GROUP)
>               fputc('\n', outfile);

Reply via email to