Skip to content

Commit 76ffa81

Browse files
committed
Merge branch 'sg/parse-options-subcommand'
The codepath for the OPT_SUBCOMMAND facility has been cleaned up. * sg/parse-options-subcommand: notes, remote: show unknown subcommands between `' notes: simplify default operation mode arguments check test-parse-options.c: fix style of comparison with zero test-parse-options.c: don't use for loop initial declaration t0040-parse-options: remove leftover debugging
2 parents 655e494 + dd834d7 commit 76ffa81

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

builtin/notes.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ static int get_ref(int argc, const char **argv, const char *prefix)
995995
int cmd_notes(int argc, const char **argv, const char *prefix)
996996
{
997997
const char *override_notes_ref = NULL;
998-
parse_opt_subcommand_fn *fn = list;
998+
parse_opt_subcommand_fn *fn = NULL;
999999
struct option options[] = {
10001000
OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
10011001
N_("use notes from <notes-ref>")),
@@ -1015,9 +1015,12 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
10151015
git_config(git_default_config, NULL);
10161016
argc = parse_options(argc, argv, prefix, options, git_notes_usage,
10171017
PARSE_OPT_SUBCOMMAND_OPTIONAL);
1018-
if (fn == list && argc && strcmp(argv[0], "list")) {
1019-
error(_("unknown subcommand: %s"), argv[0]);
1020-
usage_with_options(git_notes_usage, options);
1018+
if (!fn) {
1019+
if (argc) {
1020+
error(_("unknown subcommand: `%s'"), argv[0]);
1021+
usage_with_options(git_notes_usage, options);
1022+
}
1023+
fn = list;
10211024
}
10221025

10231026
if (override_notes_ref) {

builtin/remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,7 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
17681768
return !!fn(argc, argv, prefix);
17691769
} else {
17701770
if (argc) {
1771-
error(_("unknown subcommand: %s"), argv[0]);
1771+
error(_("unknown subcommand: `%s'"), argv[0]);
17721772
usage_with_options(builtin_remote_usage, options);
17731773
}
17741774
return !!show_all();

t/helper/test-parse-options.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ int cmd__parse_options(int argc, const char **argv)
195195

196196
static void print_args(int argc, const char **argv)
197197
{
198-
for (int i = 0; i < argc; i++)
198+
int i;
199+
for (i = 0; i < argc; i++)
199200
printf("arg %02d: %s\n", i, argv[i]);
200201
}
201202

@@ -254,7 +255,7 @@ int cmd__parse_options_flags(int argc, const char **argv)
254255
argc = parse_options(argc, argv, NULL, test_flag_options, usage,
255256
PARSE_OPT_STOP_AT_NON_OPTION);
256257

257-
if (argc == 0 || strcmp(argv[0], "cmd")) {
258+
if (!argc || strcmp(argv[0], "cmd")) {
258259
error("'cmd' is mandatory");
259260
usage_with_options(usage, test_flag_options);
260261
}
@@ -312,7 +313,7 @@ int cmd__parse_subcommand(int argc, const char **argv)
312313
argc = parse_options(argc, argv, NULL, test_flag_options, usage,
313314
PARSE_OPT_STOP_AT_NON_OPTION);
314315

315-
if (argc == 0 || strcmp(argv[0], "cmd")) {
316+
if (!argc || strcmp(argv[0], "cmd")) {
316317
error("'cmd' is mandatory");
317318
usage_with_options(usage, test_flag_options);
318319
}

t/t0040-parse-options.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ test_expect_success 'KEEP_UNKNOWN_OPT works' '
500500

501501
test_expect_success 'NO_INTERNAL_HELP works for -h' '
502502
test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
503-
cat err &&
504503
grep "^error: unknown switch \`h$SQ" err &&
505504
grep "^usage: " err
506505
'
@@ -509,7 +508,6 @@ for help_opt in help help-all
509508
do
510509
test_expect_success "NO_INTERNAL_HELP works for --$help_opt" "
511510
test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
512-
cat err &&
513511
grep '^error: unknown option \`'$help_opt\' err &&
514512
grep '^usage: ' err
515513
"

0 commit comments

Comments
 (0)