git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3] rev-parse --parseopt: option argument name hints

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 20, 2014, 18:38 UTC
Message-ID
<xmqqpplgyaud.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1395305092-1928-1-git-send-email-ilya.bobyr@gmail.com>
Ilya Bobyr <ilya.bobyr@gmail.com> writes:
Show 48 quoted lines
> Built-in commands can specify names for option arguments when usage text
> is generated for a command.  sh based commands should be able to do the
> same.
>
> Option argument name hint is any text that comes after [*=?!] after the
> argument name up to the first whitespace.  Underscores are replaced with
> whitespace.  It is unlikely that an underscore would be useful in the
> hint text.
>
> Signed-off-by: Ilya Bobyr <ilya.bobyr@gmail.com>
> ---
>  Changed according to the last comments.  Added "Usage text" paragraph in the
>  documentation and updated variable names.
>
>  Documentation/git-rev-parse.txt |   34 ++++++++++++++++++++++++++++++++--
>  builtin/rev-parse.c             |   17 ++++++++++++++++-
>  t/t1502-rev-parse-parseopt.sh   |   20 ++++++++++++++++++++
>  3 files changed, 68 insertions(+), 3 deletions(-)
>
> diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt
> index 0d2cdcd..b8aabc9 100644
> --- a/Documentation/git-rev-parse.txt
> +++ b/Documentation/git-rev-parse.txt
> @@ -284,13 +284,13 @@ Input Format
>  
>  'git rev-parse --parseopt' input format is fully text based. It has two parts,
>  separated by a line that contains only `--`. The lines before the separator
> -(should be more than one) are used for the usage.
> +(should be one or more) are used for the usage.
>  The lines after the separator describe the options.
>  
>  Each line of options has this format:
>  
>  ------------
> -<opt_spec><flags>* SP+ help LF
> +<opt_spec><flags>*<arg_hint>? SP+ help LF
>  ------------
>  
>  `<opt_spec>`::
> @@ -313,6 +313,12 @@ Each line of options has this format:
>  
>  	* Use `!` to not make the corresponding negated long option available.
>  
> +`<arg_hint>`::
> +	`<arg_hing>`, if specified, is used as a name of the argument in the
> +	help output, for options that take arguments. `<arg_hint>` is
> +	terminated by the first whitespace. When output the name is shown in
> +	angle braces.  Underscore symbols are replaced with spaces.

The last part is troubling (and sounds not very sane). Do we do such a munging anywhere else, or is it just here? If the latter I'd prefer not to see such a hack.

Show 19 quoted lines
> @@ -333,6 +339,8 @@ h,help    show the help
>  
>  foo       some nifty option --foo
>  bar=      some cool option --bar with an argument
> +baz=arg   another cool option --baz with a named argument
> +qux?path  qux may take a path argument but has meaning by itself
>  
>    An option group Header
>  C?        option C with an optional argument"
> @@ -340,6 +348,28 @@ C?        option C with an optional argument"
>  eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
>  ------------
>  
> +
> +Usage text
> +~~~~~~~~~~
> +
> +When "$@" is "-h" or "--help" the above example would produce the following
> +usage text:

Sounds like a good idea to add this; all the above arguments inside double quotes should be typeset `as-typed`, though.

Show 14 quoted lines
> @@ -419,6 +420,20 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
>  		o->value = &parsed;
>  		o->flags = PARSE_OPT_NOARG;
>  		o->callback = &parseopt_dump;
> +
> +		/* Possible argument name hint */
> +		end = s;
> +		while (s > sb.buf && strchr("*=?!", s[-1]) == NULL)
> +			--s;
> +		if (s != sb.buf && s != end) {
> +			char *a;
> +			o->argh = a = xmemdupz(s, end - s);
> +			while (a = strchr(a, '_'))
> +				*a = ' ';

... and without the "underscore" munging, we do not have to allocate a new piece of memory, either.

Previous: Ilya BobyrNext: Ilya Bobyr
Message 13 of 23 in “rev-parse --parseopt: option argument name hints”
  1. rev-parse --parseopt: option argument name hintsIlya Bobyr, Mar 3, 2014
  2. Junio C HamanoMar 4, 2014
  3. Ilya BobyrMar 10, 2014
  4. rev-parse --parseopt: option argument name hintsIlya Bobyr, Mar 10, 2014
  5. Junio C HamanoMar 10, 2014
  6. Junio C HamanoMar 11, 2014
  7. Ilya BobyrMar 12, 2014
  8. Junio C HamanoMar 12, 2014
  9. Ilya BobyrMar 19, 2014
  10. Junio C HamanoMar 19, 2014
  11. Ilya BobyrMar 20, 2014
  12. rev-parse --parseopt: option argument name hintsIlya Bobyr, Mar 20, 2014
  13. Junio C HamanoMar 20, 2014
  14. Ilya BobyrMar 20, 2014
  15. Ilya BobyrMar 21, 2014
  16. Junio C HamanoMar 21, 2014
  17. rev-parse --parseopt: option argument name hintsIlya Bobyr, Mar 22, 2014
  18. 0/3 Parse-options: spell multi-word placeholders with dashesJunio C Hamano, Mar 24, 2014
  19. 1/3 parse-options: multi-word argh should use dash to separate wordsJunio C Hamano, Mar 24, 2014
  20. 2/3 update-index: teach --cacheinfo a new syntax "mode,sha1,path"Junio C Hamano, Mar 24, 2014
  21. 3/3 parse-options: make sure argh string does not have SP or _Junio C Hamano, Mar 24, 2014
  22. Eric SunshineMar 20, 2014
  23. Ilya BobyrMar 21, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.