From 9a22b4d907ebca52352997fe0e69714db247bb4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 5 Sep 2022 20:50:03 +0200 Subject: [PATCH 1/5] t0040-parse-options: remove leftover debugging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- t/t0040-parse-options.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index b19b8d3486..5cc62306e3 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -500,7 +500,6 @@ test_expect_success 'KEEP_UNKNOWN_OPT works' ' test_expect_success 'NO_INTERNAL_HELP works for -h' ' test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err && - cat err && grep "^error: unknown switch \`h$SQ" err && grep "^usage: " err ' @@ -509,7 +508,6 @@ for help_opt in help help-all do test_expect_success "NO_INTERNAL_HELP works for --$help_opt" " test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err && - cat err && grep '^error: unknown option \`'$help_opt\' err && grep '^usage: ' err " From 6983f4e3b205a48cfcb76e14a8b275f9eb72936d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 5 Sep 2022 20:50:04 +0200 Subject: [PATCH 2/5] test-parse-options.c: don't use for loop initial declaration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We would like to eventually use for loop initial declarations in our codebase, but we are not there yet. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- t/helper/test-parse-options.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c index aa0ad45851..9fe8ce66cb 100644 --- a/t/helper/test-parse-options.c +++ b/t/helper/test-parse-options.c @@ -195,7 +195,8 @@ int cmd__parse_options(int argc, const char **argv) static void print_args(int argc, const char **argv) { - for (int i = 0; i < argc; i++) + int i; + for (i = 0; i < argc; i++) printf("arg %02d: %s\n", i, argv[i]); } From 45bec2ead294b5f3565903995d3503ba31b8ad4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 5 Sep 2022 20:50:05 +0200 Subject: [PATCH 3/5] test-parse-options.c: fix style of comparison with zero MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The preferred style is '!argc' instead of 'argc == 0'. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- t/helper/test-parse-options.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c index 9fe8ce66cb..506835521a 100644 --- a/t/helper/test-parse-options.c +++ b/t/helper/test-parse-options.c @@ -255,7 +255,7 @@ int cmd__parse_options_flags(int argc, const char **argv) argc = parse_options(argc, argv, NULL, test_flag_options, usage, PARSE_OPT_STOP_AT_NON_OPTION); - if (argc == 0 || strcmp(argv[0], "cmd")) { + if (!argc || strcmp(argv[0], "cmd")) { error("'cmd' is mandatory"); usage_with_options(usage, test_flag_options); } @@ -313,7 +313,7 @@ int cmd__parse_subcommand(int argc, const char **argv) argc = parse_options(argc, argv, NULL, test_flag_options, usage, PARSE_OPT_STOP_AT_NON_OPTION); - if (argc == 0 || strcmp(argv[0], "cmd")) { + if (!argc || strcmp(argv[0], "cmd")) { error("'cmd' is mandatory"); usage_with_options(usage, test_flag_options); } From 1c7c25aef12918b9305df19142ab5755297274a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 5 Sep 2022 20:50:06 +0200 Subject: [PATCH 4/5] notes: simplify default operation mode arguments check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 'git notes' has a default operation mode, but when invoked without a subcommand it doesn't accept any arguments (although the 'list' subcommand implementing the default operation mode does accept arguments). The condition checking this ended up a bit awkward, so let's make it clearer. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- builtin/notes.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/builtin/notes.c b/builtin/notes.c index 42cbae4659..60410af572 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -995,7 +995,7 @@ static int get_ref(int argc, const char **argv, const char *prefix) int cmd_notes(int argc, const char **argv, const char *prefix) { const char *override_notes_ref = NULL; - parse_opt_subcommand_fn *fn = list; + parse_opt_subcommand_fn *fn = NULL; struct option options[] = { OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"), N_("use notes from ")), @@ -1015,9 +1015,12 @@ int cmd_notes(int argc, const char **argv, const char *prefix) git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, git_notes_usage, PARSE_OPT_SUBCOMMAND_OPTIONAL); - if (fn == list && argc && strcmp(argv[0], "list")) { - error(_("unknown subcommand: %s"), argv[0]); - usage_with_options(git_notes_usage, options); + if (!fn) { + if (argc) { + error(_("unknown subcommand: %s"), argv[0]); + usage_with_options(git_notes_usage, options); + } + fn = list; } if (override_notes_ref) { From dd834d75caabd436e306c136f753c801220db2df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Mon, 5 Sep 2022 20:50:07 +0200 Subject: [PATCH 5/5] notes, remote: show unknown subcommands between `' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update the "unknown subcommand" error message in 'git notes' and 'git remote' to wrap the offending argument between `', to make it consistent with the "unknown switch/option/subcommand" error messages in parse-options. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- builtin/notes.c | 2 +- builtin/remote.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/notes.c b/builtin/notes.c index 60410af572..be51f69225 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -1017,7 +1017,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix) PARSE_OPT_SUBCOMMAND_OPTIONAL); if (!fn) { if (argc) { - error(_("unknown subcommand: %s"), argv[0]); + error(_("unknown subcommand: `%s'"), argv[0]); usage_with_options(git_notes_usage, options); } fn = list; diff --git a/builtin/remote.c b/builtin/remote.c index 9aff864fd6..596a71af13 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -1769,7 +1769,7 @@ int cmd_remote(int argc, const char **argv, const char *prefix) return !!fn(argc, argv, prefix); } else { if (argc) { - error(_("unknown subcommand: %s"), argv[0]); + error(_("unknown subcommand: `%s'"), argv[0]); usage_with_options(builtin_remote_usage, options); } return !!show_all();