Merge branch 'sg/parse-options-subcommand'

Introduce the "subcommand" mode to parse-options API and update the
command line parser of Git commands with subcommands.

* sg/parse-options-subcommand: (23 commits)
  remote: run "remote rm" argv through parse_options()
  maintenance: add parse-options boilerplate for subcommands
  pass subcommand "prefix" arguments to parse_options()
  builtin/worktree.c: let parse-options parse subcommands
  builtin/stash.c: let parse-options parse subcommands
  builtin/sparse-checkout.c: let parse-options parse subcommands
  builtin/remote.c: let parse-options parse subcommands
  builtin/reflog.c: let parse-options parse subcommands
  builtin/notes.c: let parse-options parse subcommands
  builtin/multi-pack-index.c: let parse-options parse subcommands
  builtin/hook.c: let parse-options parse subcommands
  builtin/gc.c: let parse-options parse 'git maintenance's subcommands
  builtin/commit-graph.c: let parse-options parse subcommands
  builtin/bundle.c: let parse-options parse subcommands
  parse-options: add support for parsing subcommands
  parse-options: drop leading space from '--git-completion-helper' output
  parse-options: clarify the limitations of PARSE_OPT_NODASH
  parse-options: PARSE_OPT_KEEP_UNKNOWN only applies to --options
  api-parse-options.txt: fix description of OPT_CMDMODE
  t0040-parse-options: test parse_options() with various 'parse_opt_flags'
  ...
This commit is contained in:
Junio C Hamano
2022-09-01 13:40:18 -07:00
35 changed files with 873 additions and 333 deletions

View File

@ -192,3 +192,130 @@ int cmd__parse_options(int argc, const char **argv)
return ret;
}
static void print_args(int argc, const char **argv)
{
for (int i = 0; i < argc; i++)
printf("arg %02d: %s\n", i, argv[i]);
}
static int parse_options_flags__cmd(int argc, const char **argv,
enum parse_opt_flags test_flags)
{
const char *usage[] = {
"<...> cmd [options]",
NULL
};
int opt = 0;
const struct option options[] = {
OPT_INTEGER('o', "opt", &opt, "an integer option"),
OPT_END()
};
argc = parse_options(argc, argv, NULL, options, usage, test_flags);
printf("opt: %d\n", opt);
print_args(argc, argv);
return 0;
}
static enum parse_opt_flags test_flags = 0;
static const struct option test_flag_options[] = {
OPT_GROUP("flag-options:"),
OPT_BIT(0, "keep-dashdash", &test_flags,
"pass PARSE_OPT_KEEP_DASHDASH to parse_options()",
PARSE_OPT_KEEP_DASHDASH),
OPT_BIT(0, "stop-at-non-option", &test_flags,
"pass PARSE_OPT_STOP_AT_NON_OPTION to parse_options()",
PARSE_OPT_STOP_AT_NON_OPTION),
OPT_BIT(0, "keep-argv0", &test_flags,
"pass PARSE_OPT_KEEP_ARGV0 to parse_options()",
PARSE_OPT_KEEP_ARGV0),
OPT_BIT(0, "keep-unknown-opt", &test_flags,
"pass PARSE_OPT_KEEP_UNKNOWN_OPT to parse_options()",
PARSE_OPT_KEEP_UNKNOWN_OPT),
OPT_BIT(0, "no-internal-help", &test_flags,
"pass PARSE_OPT_NO_INTERNAL_HELP to parse_options()",
PARSE_OPT_NO_INTERNAL_HELP),
OPT_BIT(0, "subcommand-optional", &test_flags,
"pass PARSE_OPT_SUBCOMMAND_OPTIONAL to parse_options()",
PARSE_OPT_SUBCOMMAND_OPTIONAL),
OPT_END()
};
int cmd__parse_options_flags(int argc, const char **argv)
{
const char *usage[] = {
"test-tool parse-options-flags [flag-options] cmd [options]",
NULL
};
argc = parse_options(argc, argv, NULL, test_flag_options, usage,
PARSE_OPT_STOP_AT_NON_OPTION);
if (argc == 0 || strcmp(argv[0], "cmd")) {
error("'cmd' is mandatory");
usage_with_options(usage, test_flag_options);
}
return parse_options_flags__cmd(argc, argv, test_flags);
}
static int subcmd_one(int argc, const char **argv, const char *prefix)
{
printf("fn: subcmd_one\n");
print_args(argc, argv);
return 0;
}
static int subcmd_two(int argc, const char **argv, const char *prefix)
{
printf("fn: subcmd_two\n");
print_args(argc, argv);
return 0;
}
static int parse_subcommand__cmd(int argc, const char **argv,
enum parse_opt_flags test_flags)
{
const char *usage[] = {
"<...> cmd subcmd-one",
"<...> cmd subcmd-two",
NULL
};
parse_opt_subcommand_fn *fn = NULL;
int opt = 0;
struct option options[] = {
OPT_SUBCOMMAND("subcmd-one", &fn, subcmd_one),
OPT_SUBCOMMAND("subcmd-two", &fn, subcmd_two),
OPT_INTEGER('o', "opt", &opt, "an integer option"),
OPT_END()
};
if (test_flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
fn = subcmd_one;
argc = parse_options(argc, argv, NULL, options, usage, test_flags);
printf("opt: %d\n", opt);
return fn(argc, argv, NULL);
}
int cmd__parse_subcommand(int argc, const char **argv)
{
const char *usage[] = {
"test-tool parse-subcommand [flag-options] cmd <subcommand>",
NULL
};
argc = parse_options(argc, argv, NULL, test_flag_options, usage,
PARSE_OPT_STOP_AT_NON_OPTION);
if (argc == 0 || strcmp(argv[0], "cmd")) {
error("'cmd' is mandatory");
usage_with_options(usage, test_flag_options);
}
return parse_subcommand__cmd(argc, argv, test_flags);
}

View File

@ -24,7 +24,7 @@ int cmd__serve_v2(int argc, const char **argv)
/* ignore all unknown cmdline switches for now */
argc = parse_options(argc, argv, prefix, options, serve_usage,
PARSE_OPT_KEEP_DASHDASH |
PARSE_OPT_KEEP_UNKNOWN);
PARSE_OPT_KEEP_UNKNOWN_OPT);
if (advertise_capabilities)
protocol_v2_advertise_capabilities();

View File

@ -51,7 +51,9 @@ static struct test_cmd cmds[] = {
{ "online-cpus", cmd__online_cpus },
{ "pack-mtimes", cmd__pack_mtimes },
{ "parse-options", cmd__parse_options },
{ "parse-options-flags", cmd__parse_options_flags },
{ "parse-pathspec-file", cmd__parse_pathspec_file },
{ "parse-subcommand", cmd__parse_subcommand },
{ "partial-clone", cmd__partial_clone },
{ "path-utils", cmd__path_utils },
{ "pcre2-config", cmd__pcre2_config },

View File

@ -41,7 +41,9 @@ int cmd__oidtree(int argc, const char **argv);
int cmd__online_cpus(int argc, const char **argv);
int cmd__pack_mtimes(int argc, const char **argv);
int cmd__parse_options(int argc, const char **argv);
int cmd__parse_options_flags(int argc, const char **argv);
int cmd__parse_pathspec_file(int argc, const char** argv);
int cmd__parse_subcommand(int argc, const char **argv);
int cmd__partial_clone(int argc, const char **argv);
int cmd__path_utils(int argc, const char **argv);
int cmd__pcre2_config(int argc, const char **argv);

View File

@ -456,4 +456,259 @@ test_expect_success '--end-of-options treats remainder as args' '
--end-of-options --verbose
'
test_expect_success 'KEEP_DASHDASH works' '
test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
cat >expect <<-\EOF &&
opt: 1
arg 00: --
arg 01: --opt=2
arg 02: --unknown
EOF
test_cmp expect actual
'
test_expect_success 'KEEP_ARGV0 works' '
test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
cat >expect <<-\EOF &&
opt: 3
arg 00: cmd
arg 01: arg0
EOF
test_cmp expect actual
'
test_expect_success 'STOP_AT_NON_OPTION works' '
test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
cat >expect <<-\EOF &&
opt: 4
arg 00: arg0
arg 01: --opt=5
arg 02: --unknown
EOF
test_cmp expect actual
'
test_expect_success 'KEEP_UNKNOWN_OPT works' '
test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
cat >expect <<-\EOF &&
opt: 6
arg 00: --unknown=1
arg 01: -u2
EOF
test_cmp expect actual
'
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
'
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
"
done
test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
cat >expect <<-\EOF &&
opt: 0
arg 00: -h
arg 01: --help
arg 02: --help-all
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - no subcommand shows error and usage' '
test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
grep "^error: need a subcommand" err &&
grep ^usage: err
'
test_expect_success 'subcommand - subcommand after -- shows error and usage' '
test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
grep "^error: need a subcommand" err &&
grep ^usage: err
'
test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' '
test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
grep "^error: need a subcommand" err &&
grep ^usage: err
'
test_expect_success 'subcommand - unknown subcommand shows error and usage' '
test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
grep "^error: unknown subcommand: \`nope$SQ" err &&
grep ^usage: err
'
test_expect_success 'subcommand - subcommands cannot be abbreviated' '
test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
grep ^usage: err
'
test_expect_success 'subcommand - no negated subcommands' '
test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
grep ^usage: err
'
test_expect_success 'subcommand - simple' '
test-tool parse-subcommand cmd subcmd-two >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_two
arg 00: subcmd-two
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - stop parsing at the first subcommand' '
test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
cat >expect <<-\EOF &&
opt: 1
fn: subcmd_two
arg 00: subcmd-two
arg 01: subcmd-one
arg 02: --opt=2
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - KEEP_ARGV0' '
test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_two
arg 00: cmd
arg 01: subcmd-two
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
test-tool parse-subcommand --subcommand-optional cmd >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_one
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_two
arg 00: subcmd-two
arg 01: branch
arg 02: file
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_one
arg 00: branch
arg 01: file
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
grep "^error: unknown option" err &&
grep ^usage: err
'
test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_one
arg 00: --subcommand-opt
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_one
arg 00: --subcommand-opt
arg 01: subcmd-two
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_one
arg 00: --subcommand-opt
arg 01: branch
arg 02: --opt=1
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_one
arg 00: cmd
arg 01: --subcommand-opt
arg 02: branch
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
cat >expect <<-\EOF &&
opt: 0
fn: subcmd_one
arg 00: --
arg 01: --subcommand-opt
arg 02: file
EOF
test_cmp expect actual
'
test_expect_success 'subcommand - completion helper' '
test-tool parse-subcommand cmd --git-completion-helper >actual &&
echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
test_cmp expect actual
'
test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' '
test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
grep ^BUG err
'
test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
grep ^BUG err
'
test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
grep ^BUG err
'
test_done

View File

@ -505,6 +505,11 @@ test_expect_success 'list notes with "git notes"' '
test_cmp expect actual
'
test_expect_success '"git notes" without subcommand does not take arguments' '
test_expect_code 129 git notes HEAD^^ 2>err &&
grep "^error: unknown subcommand" err
'
test_expect_success 'list specific note with "git notes list <object>"' '
git rev-parse refs/notes/commits:$commit_3 >expect &&
git notes list HEAD^^ >actual &&

View File

@ -25,7 +25,7 @@ test_expect_success 'usage on main command -h emits a summary of subcommands' '
grep -F "or: git stash show" usage
'
test_expect_failure 'usage for subcommands should emit subcommand usage' '
test_expect_success 'usage for subcommands should emit subcommand usage' '
test_expect_code 129 git stash push -h >usage &&
grep -F "usage: git stash [push" usage
'

View File

@ -12,12 +12,12 @@ test_expect_success 'usage' '
test_expect_success 'usage shown without sub-command' '
test_expect_code 129 git commit-graph 2>err &&
! grep error: err
grep usage: err
'
test_expect_success 'usage shown with an error on unknown sub-command' '
cat >expect <<-\EOF &&
error: unrecognized subcommand: unknown
error: unknown subcommand: `unknown'\''
EOF
test_expect_code 129 git commit-graph unknown 2>stderr &&
grep error stderr >actual &&

View File

@ -241,6 +241,26 @@ test_expect_success 'add invalid foreign_vcs remote' '
test_cmp expect actual
'
test_expect_success 'without subcommand' '
echo origin >expect &&
git -C test remote >actual &&
test_cmp expect actual
'
test_expect_success 'without subcommand accepts -v' '
cat >expect <<-EOF &&
origin $(pwd)/one (fetch)
origin $(pwd)/one (push)
EOF
git -C test remote -v >actual &&
test_cmp expect actual
'
test_expect_success 'without subcommand does not take arguments' '
test_expect_code 129 git -C test remote origin 2>err &&
grep "^error: unknown subcommand:" err
'
cat >test/expect <<EOF
* remote origin
Fetch URL: $(pwd)/one

View File

@ -32,11 +32,13 @@ test_systemd_analyze_verify () {
}
test_expect_success 'help text' '
test_expect_code 129 git maintenance -h 2>err &&
test_i18ngrep "usage: git maintenance <subcommand>" err &&
test_expect_code 128 git maintenance barf 2>err &&
test_i18ngrep "invalid subcommand: barf" err &&
test_expect_code 129 git maintenance -h >actual &&
test_i18ngrep "usage: git maintenance <subcommand>" actual &&
test_expect_code 129 git maintenance barf 2>err &&
test_i18ngrep "unknown subcommand: \`barf'\''" err &&
test_i18ngrep "usage: git maintenance" err &&
test_expect_code 129 git maintenance 2>err &&
test_i18ngrep "error: need a subcommand" err &&
test_i18ngrep "usage: git maintenance" err
'