Skip to content

Commit 9c7304e

Browse files
giuseppegitster
authored andcommitted
print the usage string on stdout instead of stderr
When -h is used, print usage messages on stdout. If a command is invoked with wrong arguments then print the usage messages on stderr. Signed-off-by: Giuseppe Scrivano <gscrivano@gnu.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 81fa024 commit 9c7304e

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

parse-options.c

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "color.h"
66

77
static int parse_options_usage(const char * const *usagestr,
8-
const struct option *opts);
8+
const struct option *opts, int err);
99

1010
#define OPT_SHORT 1
1111
#define OPT_UNSET 2
@@ -352,7 +352,7 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
352352
}
353353

354354
static int usage_with_options_internal(const char * const *,
355-
const struct option *, int);
355+
const struct option *, int, int);
356356

357357
int parse_options_step(struct parse_opt_ctx_t *ctx,
358358
const struct option *options,
@@ -380,21 +380,21 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
380380
if (arg[1] != '-') {
381381
ctx->opt = arg + 1;
382382
if (internal_help && *ctx->opt == 'h')
383-
return parse_options_usage(usagestr, options);
383+
return parse_options_usage(usagestr, options, 0);
384384
switch (parse_short_opt(ctx, options)) {
385385
case -1:
386-
return parse_options_usage(usagestr, options);
386+
return parse_options_usage(usagestr, options, 1);
387387
case -2:
388388
goto unknown;
389389
}
390390
if (ctx->opt)
391391
check_typos(arg + 1, options);
392392
while (ctx->opt) {
393393
if (internal_help && *ctx->opt == 'h')
394-
return parse_options_usage(usagestr, options);
394+
return parse_options_usage(usagestr, options, 0);
395395
switch (parse_short_opt(ctx, options)) {
396396
case -1:
397-
return parse_options_usage(usagestr, options);
397+
return parse_options_usage(usagestr, options, 1);
398398
case -2:
399399
/* fake a short option thing to hide the fact that we may have
400400
* started to parse aggregated stuff
@@ -418,12 +418,12 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
418418
}
419419

420420
if (internal_help && !strcmp(arg + 2, "help-all"))
421-
return usage_with_options_internal(usagestr, options, 1);
421+
return usage_with_options_internal(usagestr, options, 1, 0);
422422
if (internal_help && !strcmp(arg + 2, "help"))
423-
return parse_options_usage(usagestr, options);
423+
return parse_options_usage(usagestr, options, 0);
424424
switch (parse_long_opt(ctx, arg + 2, options)) {
425425
case -1:
426-
return parse_options_usage(usagestr, options);
426+
return parse_options_usage(usagestr, options, 1);
427427
case -2:
428428
goto unknown;
429429
}
@@ -468,7 +468,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
468468
return parse_options_end(&ctx);
469469
}
470470

471-
static int usage_argh(const struct option *opts)
471+
static int usage_argh(const struct option *opts, FILE *outfile)
472472
{
473473
const char *s;
474474
int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
@@ -479,80 +479,82 @@ static int usage_argh(const struct option *opts)
479479
s = literal ? "[%s]" : "[<%s>]";
480480
else
481481
s = literal ? " %s" : " <%s>";
482-
return fprintf(stderr, s, opts->argh ? opts->argh : "...");
482+
return fprintf(outfile, s, opts->argh ? opts->argh : "...");
483483
}
484484

485485
#define USAGE_OPTS_WIDTH 24
486486
#define USAGE_GAP 2
487487

488488
static int usage_with_options_internal(const char * const *usagestr,
489-
const struct option *opts, int full)
489+
const struct option *opts, int full, int err)
490490
{
491+
FILE *outfile = err ? stderr : stdout;
492+
491493
if (!usagestr)
492494
return PARSE_OPT_HELP;
493495

494-
fprintf(stderr, "usage: %s\n", *usagestr++);
496+
fprintf(outfile, "usage: %s\n", *usagestr++);
495497
while (*usagestr && **usagestr)
496-
fprintf(stderr, " or: %s\n", *usagestr++);
498+
fprintf(outfile, " or: %s\n", *usagestr++);
497499
while (*usagestr) {
498-
fprintf(stderr, "%s%s\n",
500+
fprintf(outfile, "%s%s\n",
499501
**usagestr ? " " : "",
500502
*usagestr);
501503
usagestr++;
502504
}
503505

504506
if (opts->type != OPTION_GROUP)
505-
fputc('\n', stderr);
507+
fputc('\n', outfile);
506508

507509
for (; opts->type != OPTION_END; opts++) {
508510
size_t pos;
509511
int pad;
510512

511513
if (opts->type == OPTION_GROUP) {
512-
fputc('\n', stderr);
514+
fputc('\n', outfile);
513515
if (*opts->help)
514-
fprintf(stderr, "%s\n", opts->help);
516+
fprintf(outfile, "%s\n", opts->help);
515517
continue;
516518
}
517519
if (!full && (opts->flags & PARSE_OPT_HIDDEN))
518520
continue;
519521

520-
pos = fprintf(stderr, " ");
522+
pos = fprintf(outfile, " ");
521523
if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
522524
if (opts->flags & PARSE_OPT_NODASH)
523-
pos += fprintf(stderr, "%c", opts->short_name);
525+
pos += fprintf(outfile, "%c", opts->short_name);
524526
else
525-
pos += fprintf(stderr, "-%c", opts->short_name);
527+
pos += fprintf(outfile, "-%c", opts->short_name);
526528
}
527529
if (opts->long_name && opts->short_name)
528-
pos += fprintf(stderr, ", ");
530+
pos += fprintf(outfile, ", ");
529531
if (opts->long_name)
530-
pos += fprintf(stderr, "--%s%s",
532+
pos += fprintf(outfile, "--%s%s",
531533
(opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
532534
opts->long_name);
533535
if (opts->type == OPTION_NUMBER)
534-
pos += fprintf(stderr, "-NUM");
536+
pos += fprintf(outfile, "-NUM");
535537

536538
if (!(opts->flags & PARSE_OPT_NOARG))
537-
pos += usage_argh(opts);
539+
pos += usage_argh(opts, outfile);
538540

539541
if (pos <= USAGE_OPTS_WIDTH)
540542
pad = USAGE_OPTS_WIDTH - pos;
541543
else {
542-
fputc('\n', stderr);
544+
fputc('\n', outfile);
543545
pad = USAGE_OPTS_WIDTH;
544546
}
545-
fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
547+
fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
546548
}
547-
fputc('\n', stderr);
549+
fputc('\n', outfile);
548550

549551
return PARSE_OPT_HELP;
550552
}
551553

552554
void usage_with_options(const char * const *usagestr,
553555
const struct option *opts)
554556
{
555-
usage_with_options_internal(usagestr, opts, 0);
557+
usage_with_options_internal(usagestr, opts, 0, 1);
556558
exit(129);
557559
}
558560

@@ -565,9 +567,9 @@ void usage_msg_opt(const char *msg,
565567
}
566568

567569
static int parse_options_usage(const char * const *usagestr,
568-
const struct option *opts)
570+
const struct option *opts, int err)
569571
{
570-
return usage_with_options_internal(usagestr, opts, 0);
572+
return usage_with_options_internal(usagestr, opts, 0, err);
571573
}
572574

573575

t/t0040-parse-options.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test_description='our own option parser'
77

88
. ./test-lib.sh
99

10-
cat > expect.err << EOF
10+
cat > expect << EOF
1111
usage: test-parse-options <options>
1212
1313
-b, --boolean get a boolean
@@ -46,10 +46,12 @@ EOF
4646

4747
test_expect_success 'test help' '
4848
test_must_fail test-parse-options -h > output 2> output.err &&
49-
test ! -s output &&
50-
test_cmp expect.err output.err
49+
test ! -s output.err &&
50+
test_cmp expect output
5151
'
5252

53+
mv expect expect.err
54+
5355
cat > expect << EOF
5456
boolean: 2
5557
integer: 1729

t/t1502-rev-parse-parseopt.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
test_description='test git rev-parse --parseopt'
44
. ./test-lib.sh
55

6-
cat > expect.err <<EOF
6+
cat > expect <<EOF
77
usage: some-command [options] <args>...
88
99
some-command does foo and bar!
@@ -38,8 +38,8 @@ extra1 line above used to cause a segfault but no longer does
3838
EOF
3939

4040
test_expect_success 'test --parseopt help output' '
41-
git rev-parse --parseopt -- -h 2> output.err < optionspec
42-
test_cmp expect.err output.err
41+
git rev-parse --parseopt -- -h > output < optionspec
42+
test_cmp expect output
4343
'
4444

4545
cat > expect <<EOF

0 commit comments

Comments
 (0)