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

[PATCH v2] short help: allow multi-line opthelp

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 18, 2023, 22:54 UTC
Message-ID
<xmqqttu0esqb.fsf@gitster.g>
In-Reply-To
<xmqq5y6gg8fn.fsf@gitster.g>

When "-h" triggers the short-help in a command that implements its option parsing using the parse-options API, the option help text is shown with a single fprintf() as a long line. When the text is multi-line, the second and subsequent lines are not left padded, that breaks the alignment across options.

Borrowing the idea from the advice API where its hint strings are shown with (localized) "hint:" prefix, let's internally split the (localized) help text into lines, and showing the first line, pad the remaining lines to align.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * With a new test.
 parse-options.c          | 12 +++++++++++-
 t/t0040-parse-options.sh |  2 ++
 2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/parse-options.c b/parse-options.c
index f8a155ee13..817416db99 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -1109,6 +1109,7 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
 	for (; opts->type != OPTION_END; opts++) {
 		size_t pos;
 		int pad;
+		const char *cp, *np;
 
 		if (opts->type == OPTION_SUBCOMMAND)
 			continue;
@@ -1157,7 +1158,16 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
 				   (const char *)opts->value);
 			continue;
 		}
-		fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
+
+		for (cp = _(opts->help); *cp; cp = np) {
+			np = strchrnul(cp, '\n');
+			fprintf(outfile,
+				"%*s%.*s\n", pad + USAGE_GAP, "",
+				(int)(np - cp), cp);
+			if (*np)
+				np++;
+			pad = USAGE_OPTS_WIDTH;
+		}
 	}
 	fputc('\n', outfile);
 
diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
index 7d7ecfd571..83e5d4eeb6 100755
--- a/t/t0040-parse-options.sh
+++ b/t/t0040-parse-options.sh
@@ -35,6 +35,8 @@ String options
     --string2 <str>       get another string
     --st <st>             get another string (pervert ordering)
     -o <str>              get another string
+    --longhelp            help text of this entry
+                          spans multiple lines
     --list <str>          add str to list
 
 Magic arguments
-- 
2.41.0-376-gcba07a324d
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 6 in “short help: allow multi-line opthelp”
  1. short help: allow multi-line opthelpJunio C Hamano, Jul 18, 2023
  2. remote: simplify "remote add --tags" help textJunio C Hamano, Jul 18, 2023
  3. short help: allow multi-line opthelpJunio C Hamano, Jul 18, 2023
  4. [RFC] short help: allow a gap smaller than USAGE_GAPJunio C Hamano, Jul 18, 2023
  5. Beat BolliJul 19, 2023
  6. Junio C HamanoJul 19, 2023

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.