All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] short help: allow multi-line opthelp
@ 2023-07-18 22:29 Junio C Hamano
  2023-07-18 22:31 ` [PATCH] remote: simplify "remote add --tags" help text Junio C Hamano
  2023-07-18 22:54 ` [PATCH v2] short help: allow multi-line opthelp Junio C Hamano
  0 siblings, 2 replies; 6+ messages in thread
From: Junio C Hamano @ 2023-07-18 22:29 UTC (permalink / raw)
  To: git

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>
---
 parse-options.c | 12 +++++++++++-
 1 file changed, 11 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);
 
-- 
2.41.0-376-gcba07a324d


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-07-19 23:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-18 22:29 [PATCH] short help: allow multi-line opthelp Junio C Hamano
2023-07-18 22:31 ` [PATCH] remote: simplify "remote add --tags" help text Junio C Hamano
2023-07-18 22:54 ` [PATCH v2] short help: allow multi-line opthelp Junio C Hamano
2023-07-18 22:58   ` [RFC] short help: allow a gap smaller than USAGE_GAP Junio C Hamano
2023-07-19 22:04     ` Beat Bolli
2023-07-19 23:26       ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.