Skip to content

Commit cdaf12f

Browse files
bk2204gitster
authored andcommitted
parse-options: exit 0 on -h
The standard philosophy for Unix software when a help option (such as --help) is specified is that the software should exit 0, printing the help output to standard output, since the standard output is for user-requested output and the program performed the requested task successfully. If the user specifies an incorrect option, then the help output should be printed to standard error (since the user has made a mistake) and it should exit unsuccessfully. Most of our commands currently exit 129 on receiving the -h option to print the short help, which does not line up with the standard philosophy above. Let's change that to exit 0 instead. This requires changes to a variety of tests which previously wanted the 129 exit code, so update them. Note that because git diff does its own option parsing, it still exits with 129, so update some of the tests to expect either exit status. Some commands also now pass with -h but not --help-all, so handle those cases differently for those commands. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 06f9b95 commit cdaf12f

37 files changed

Lines changed: 85 additions & 71 deletions

builtin/blame.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ int cmd_blame(int argc,
10131013
case PARSE_OPT_UNKNOWN:
10141014
break;
10151015
case PARSE_OPT_HELP:
1016+
exit(0);
10161017
case PARSE_OPT_HELP_ERROR:
10171018
case PARSE_OPT_ERROR:
10181019
case PARSE_OPT_SUBCOMMAND:

builtin/shortlog.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ int cmd_shortlog(int argc,
433433
case PARSE_OPT_UNKNOWN:
434434
break;
435435
case PARSE_OPT_HELP:
436+
exit(0);
436437
case PARSE_OPT_HELP_ERROR:
437438
case PARSE_OPT_ERROR:
438439
case PARSE_OPT_SUBCOMMAND:

builtin/update-index.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ int cmd_update_index(int argc,
11331133
break;
11341134
switch (parseopt_state) {
11351135
case PARSE_OPT_HELP:
1136+
exit(0);
11361137
case PARSE_OPT_HELP_ERROR:
11371138
case PARSE_OPT_ERROR:
11381139
exit(129);

contrib/subtree/t/t7900-subtree.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ test_create_subtree_add () {
9999
}
100100

101101
test_expect_success 'shows short help text for -h' '
102-
test_expect_code 129 git subtree -h >out 2>err &&
102+
git subtree -h >out 2>err &&
103103
test_must_be_empty err &&
104104
grep -e "^ *or: git subtree pull" out &&
105105
grep -F -e "--[no-]annotate" out

parse-options.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,8 +1135,9 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
11351135
case PARSE_OPT_UNKNOWN:
11361136
goto unknown;
11371137
case PARSE_OPT_HELP:
1138-
case PARSE_OPT_HELP_ERROR:
11391138
goto show_usage;
1139+
case PARSE_OPT_HELP_ERROR:
1140+
goto show_usage_stderr;
11401141
case PARSE_OPT_NON_OPTION:
11411142
case PARSE_OPT_SUBCOMMAND:
11421143
case PARSE_OPT_COMPLETE:
@@ -1170,6 +1171,9 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
11701171
show_usage:
11711172
return usage_with_options_internal(ctx, usagestr, options,
11721173
USAGE_NORMAL, USAGE_TO_STDOUT);
1174+
show_usage_stderr:
1175+
return usage_with_options_internal(ctx, usagestr, options,
1176+
USAGE_NORMAL, USAGE_TO_STDERR);
11731177
}
11741178

11751179
int parse_options_end(struct parse_opt_ctx_t *ctx)
@@ -1201,6 +1205,7 @@ int parse_options(int argc, const char **argv,
12011205
parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
12021206
switch (parse_options_step(&ctx, options, usagestr)) {
12031207
case PARSE_OPT_HELP:
1208+
exit(0);
12041209
case PARSE_OPT_HELP_ERROR:
12051210
case PARSE_OPT_ERROR:
12061211
exit(129);
@@ -1500,11 +1505,11 @@ void show_usage_with_options_if_asked(int ac, const char **av,
15001505
if (!strcmp(av[1], "-h")) {
15011506
usage_with_options_internal(NULL, usagestr, opts,
15021507
USAGE_NORMAL, USAGE_TO_STDOUT);
1503-
exit(129);
1508+
exit(0);
15041509
} else if (!strcmp(av[1], "--help-all")) {
15051510
usage_with_options_internal(NULL, usagestr, opts,
15061511
USAGE_FULL, USAGE_TO_STDOUT);
1507-
exit(129);
1512+
exit(0);
15081513
}
15091514
}
15101515
}

t/for-each-ref-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ test_expect_success 'Verify descending sort' '
522522
'
523523

524524
test_expect_success 'Give help even with invalid sort atoms' '
525-
test_expect_code 129 ${git_for_each_ref} --sort=bogus -h >actual 2>&1 &&
525+
${git_for_each_ref} --sort=bogus -h >actual 2>&1 &&
526526
grep "^usage: ${git_for_each_ref}" actual
527527
'
528528

t/t0012-help.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ do
260260
(
261261
GIT_CEILING_DIRECTORIES=$(pwd) &&
262262
export GIT_CEILING_DIRECTORIES &&
263-
test_expect_code 129 git -C sub $builtin -h >output 2>err
263+
git -C sub $builtin -h >output 2>err
264264
) &&
265265
test_must_be_empty err &&
266266
test_grep usage output

t/t0040-parse-options.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Alias
6868
EOF
6969

7070
test_expect_success 'test help' '
71-
test_must_fail test-tool parse-options -h >output 2>output.err &&
71+
test-tool parse-options -h >output 2>output.err &&
7272
test_must_be_empty output.err &&
7373
test_cmp expect output
7474
'

t/t0450-txt-doc-vs-help.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ help_to_synopsis () {
2929
return 0
3030
fi &&
3131
mkdir -p "$out_dir" &&
32-
test_expect_code 129 git $builtin -h >"$out.raw" 2>&1 &&
32+
test_might_fail git $builtin -h >"$out.raw" 2>&1 &&
3333
sed -n \
3434
-e '1,/^$/ {
3535
/^$/d;

t/t0610-reftable-basics.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export GIT_TEST_DEFAULT_REF_FORMAT
1515
INVALID_OID=$(test_oid 001)
1616

1717
test_expect_success 'pack-refs does not crash with -h' '
18-
test_expect_code 129 git pack-refs -h >usage &&
18+
git pack-refs -h >usage &&
1919
test_grep "[Uu]sage: git pack-refs " usage &&
20-
test_expect_code 129 nongit git pack-refs -h >usage &&
20+
nongit git pack-refs -h >usage &&
2121
test_grep "[Uu]sage: git pack-refs " usage
2222
'
2323

0 commit comments

Comments
 (0)