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

[PATCH v2 1/4] parse-options: fix completion format when first option is skipped

From
Patrick Steinhardt <ps@pks.im>
Date
Oct 2, 2026, 08:09 UTC
Message-ID
<20261002-b4-pks-parse-options-subcommand-groups-v2-1-3299bee52dea@pks.im>
In-Reply-To
<20261002-b4-pks-parse-options-subcommand-groups-v2-0-3299bee52dea@pks.im>

The "--git-completion-helper" option can be passed to any command or subcommand that uses the parse-options interface. The output it generates is a space-separated list of subcommands or options understood by the command.

The format is slightly broken though in the case where the first option is not being printed, like for example a group or a hidden option. In that case, `show_gitcomp()` will of course skip that first entry. But when printing the next option it checks for `opts == original_opts` to verify whether we're printing the first option. The check will evaluate to false though as we have skipped it, and thus we'll print a leading space even though we have printed nothing else yet.

Fix that bug by tracking whether we have already printed anything via a local variable.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 parse-options.c               | 4 +++-
 t/helper/test-parse-options.c | 1 +
 2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/parse-options.c b/parse-options.c
index 4519ead9dc..8bb30ec116 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -845,6 +845,7 @@ static int show_gitcomp(const struct option *opts, int show_all)
 {
 	const struct option *original_opts = opts;
 	int nr_noopts = 0;
+	bool shown = false;
 
 	for (; opts->type != OPTION_END; opts++) {
 		const char *prefix = "--";
@@ -882,8 +883,9 @@ static int show_gitcomp(const struct option *opts, int show_all)
 			suffix = "=";
 		if (starts_with(opts->long_name, "no-"))
 			nr_noopts++;
-		printf("%s%s%s%s", opts == original_opts ? "" : " ",
+		printf("%s%s%s%s", shown ? " " : "",
 		       prefix, opts->long_name, suffix);
+		shown = true;
 	}
 	show_negated_gitcomp(original_opts, show_all, -1);
 	show_negated_gitcomp(original_opts, show_all, nr_noopts);
diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
index f181f0c02d..fbafd67756 100644
--- a/t/helper/test-parse-options.c
+++ b/t/helper/test-parse-options.c
@@ -351,6 +351,7 @@ static int parse_subcommand__cmd(int argc, const char **argv,
 	parse_opt_subcommand_fn *fn = NULL;
 	int opt = 0;
 	struct option options[] = {
+		OPT_GROUP("Subcommands"),
 		OPT_SUBCOMMAND("subcmd-one", &fn, subcmd_one),
 		OPT_SUBCOMMAND("subcmd-two", &fn, subcmd_two),
 		OPT_INTEGER('o', "opt", &opt, "an integer option"),
-- 
2.56.0.353.g0856645cf6.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 11 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.