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

Re: [PATCH 2/3] parse-options: allow grouping subcommands

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 1, 2026, 17:46 UTC
Message-ID
<xmqqcxtt5n67.fsf@gitster.g>
In-Reply-To
<20261001-b4-pks-parse-options-subcommand-groups-v1-2-01eb2f4a4c32@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 20 quoted lines
> @@ -1432,35 +1432,39 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
>  		}
>  
>  		pos = usage_indent(outfile);
> -		if (opts->short_name) {
> -			if (opts->flags & PARSE_OPT_NODASH)
> -				pos += fprintf(outfile, "%c", opts->short_name);
> -			else
> -				pos += fprintf(outfile, "-%c", opts->short_name);
> -		}
> -		if (opts->long_name && opts->short_name)
> -			pos += fprintf(outfile, ", ");
> -		if (opts->long_name) {
> -			const char *long_name = opts->long_name;
> -			if ((opts->flags & PARSE_OPT_NONEG) ||
> -			    skip_prefix(long_name, "no-", &positive_name))
> -				pos += fprintf(outfile, "--%s", long_name);
> -			else
> -				pos += fprintf(outfile, "--[no-]%s", long_name);
> -		}

It may have made it easier to follow if a preliminary step pushed the above to a helper function. It would have also prevented the nesting becoming too deep as we see below.

Show 17 quoted lines
> +		if (opts->type == OPTION_SUBCOMMAND) {
> +			pos += fprintf(outfile, "%s", opts->long_name);
> +		} else {
> +			if (opts->short_name) {
> +				if (opts->flags & PARSE_OPT_NODASH)
> +					pos += fprintf(outfile, "%c", opts->short_name);
> +				else
> +					pos += fprintf(outfile, "-%c", opts->short_name);
> +			}
> +			if (opts->long_name && opts->short_name)
> +				pos += fprintf(outfile, ", ");
> +			if (opts->long_name) {
> +				const char *long_name = opts->long_name;
> +				if ((opts->flags & PARSE_OPT_NONEG) ||
> +				    skip_prefix(long_name, "no-", &positive_name))
> +					pos += fprintf(outfile, "--%s", long_name);
> +				else
Show 15 quoted lines
> diff --git a/parse-options.h b/parse-options.h
> index d7f896a933..5249404b46 100644
> --- a/parse-options.h
> +++ b/parse-options.h
> @@ -401,6 +401,13 @@ static char *parse_options_noop_ignored_value MAYBE_UNUSED;
>  	.subcommand_fn = (fn), \
>  }
>  #define OPT_SUBCOMMAND(l, v, fn)    OPT_SUBCOMMAND_F((l), (v), (fn), 0)
> +#define OPT_SUBCOMMAND_H(l, v, fn, h) { \
> +	.type = OPTION_SUBCOMMAND, \
> +	.long_name = (l), \
> +	.value = (v), \
> +	.help = (h), \
> +	.subcommand_fn = (fn), \
> +}

As presented, _F does not allow you to give it a help, and _H does not allow you to give it a flag word. I would have preferred to see OPT_SUBCOMMAND_F to be extended to also take the help text, as we only have two existing users in *.c code, rather than adding _H variant that is incomplete and keeping _F incomplete.

Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 6 of 14 in “builtin/refs: introduce subcommand groups”
  1. 0/3 builtin/refs: introduce subcommand groupsPatrick Steinhardt, Oct 1, 2026
  2. 1/3 parse-options: fix completion format when first option is skippedPatrick Steinhardt, Oct 1, 2026
  3. Junio C HamanoOct 1, 2026
  4. Patrick SteinhardtOct 2, 2026
  5. 2/3 parse-options: allow grouping subcommandsPatrick Steinhardt, Oct 1, 2026
  6. Junio C HamanoOct 1, 2026
  7. Patrick SteinhardtOct 2, 2026
  8. 3/3 builtin/refs: introduce subcommand groupsPatrick Steinhardt, Oct 1, 2026
  9. Junio C HamanoOct 1, 2026
  10. 0/4 builtin/refs: introduce subcommand groupsPatrick Steinhardt, Oct 2, 2026
  11. 1/4 parse-options: fix completion format when first option is skippedPatrick Steinhardt, Oct 2, 2026
  12. 2/4 parse-options: extract functions to print single optionPatrick Steinhardt, Oct 2, 2026
  13. 3/4 parse-options: allow grouping subcommandsPatrick Steinhardt, Oct 2, 2026
  14. 4/4 builtin/refs: introduce subcommand groupsPatrick Steinhardt, Oct 2, 2026

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.