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
This commit is contained in:
Junio C Hamano 2022-09-13 11:38:24 -07:00
commit 76ffa818c7
4 changed files with 12 additions and 10 deletions

View File

@ -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 <notes-ref>")),
@ -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) {

View File

@ -1768,7 +1768,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();

View File

@ -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]);
}
@ -254,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);
}
@ -312,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);
}

View File

@ -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
"