]> git.ipfire.org Git - thirdparty/git.git/commitdiff
short help: allow multi-line opthelp
authorJunio C Hamano <gitster@pobox.com>
Tue, 18 Jul 2023 22:54:04 +0000 (15:54 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 19 Jul 2023 23:30:06 +0000 (16:30 -0700)
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
t/helper/test-parse-options.c
t/t0040-parse-options.sh

index f8a155ee13b9599e2de3f973d9f9744c9c045893..817416db99c584842a705a2741a28d44ff778a3e 100644 (file)
@@ -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);
 
index 00fa281a9c9342d2ec4213665bac8f6b789c8b90..a4f6e24b0c6e1bf6557803921b5e9b21853791a3 100644 (file)
@@ -133,6 +133,8 @@ int cmd__parse_options(int argc, const char **argv)
                OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
                OPT_STRING('o', NULL, &string, "str", "get another string"),
                OPT_NOOP_NOARG(0, "obsolete"),
+               OPT_SET_INT_F(0, "longhelp", &integer, "help text of this entry\n"
+                             "spans multiple lines", 0, PARSE_OPT_NONEG),
                OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
                OPT_GROUP("Magic arguments"),
                OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
index 7d7ecfd57162cf9cde2e74007a6323c635a3b2a9..83e5d4eeb68105571f9a43c72172164b174e596c 100755 (executable)
@@ -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