Skip to content

Commit a6304fa

Browse files
drafnelgitster
authored andcommitted
parse-options: only insert newline in help text if needed
Currently, when parse_options() produces a help message it always emits a blank line after the usage text to separate it from the options text. If the option spec does not define any switches, or only defines hidden switches that will not be displayed, then the help text will end up with two trailing blank lines instead of one. Let's defer emitting the blank line between the usage text and the options text until it is clear that the options section will not be empty. Fixes t1502.5, t1502.6. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1a9bf1e commit a6304fa

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

parse-options.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
581581
const struct option *opts, int full, int err)
582582
{
583583
FILE *outfile = err ? stderr : stdout;
584+
int need_newline;
584585

585586
if (!usagestr)
586587
return PARSE_OPT_HELP;
@@ -603,22 +604,27 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
603604
usagestr++;
604605
}
605606

606-
if (opts->type != OPTION_GROUP)
607-
fputc('\n', outfile);
607+
need_newline = 1;
608608

609609
for (; opts->type != OPTION_END; opts++) {
610610
size_t pos;
611611
int pad;
612612

613613
if (opts->type == OPTION_GROUP) {
614614
fputc('\n', outfile);
615+
need_newline = 0;
615616
if (*opts->help)
616617
fprintf(outfile, "%s\n", _(opts->help));
617618
continue;
618619
}
619620
if (!full && (opts->flags & PARSE_OPT_HIDDEN))
620621
continue;
621622

623+
if (need_newline) {
624+
fputc('\n', outfile);
625+
need_newline = 0;
626+
}
627+
622628
pos = fprintf(outfile, " ");
623629
if (opts->short_name) {
624630
if (opts->flags & PARSE_OPT_NODASH)

t/t1502-rev-parse-parseopt.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ END_EXPECT
9898
test_i18ncmp expect output
9999
'
100100

101-
test_expect_failure 'test --parseopt help output no switches' '
101+
test_expect_success 'test --parseopt help output no switches' '
102102
sed -e "s/^|//" >expect <<\END_EXPECT &&
103103
|cat <<\EOF
104104
|usage: some-command [options] <args>...
@@ -111,7 +111,7 @@ END_EXPECT
111111
test_i18ncmp expect output
112112
'
113113

114-
test_expect_failure 'test --parseopt help output hidden switches' '
114+
test_expect_success 'test --parseopt help output hidden switches' '
115115
sed -e "s/^|//" >expect <<\END_EXPECT &&
116116
|cat <<\EOF
117117
|usage: some-command [options] <args>...

0 commit comments

Comments
 (0)