Skip to content

Commit fa83cc8

Browse files
szedergitster
authored andcommitted
parse-options: add support for parsing subcommands
Several Git commands have subcommands to implement mutually exclusive "operation modes", and they usually parse their subcommand argument with a bunch of if-else if statements. Teach parse-options to handle subcommands as well, which will result in shorter and simpler code with consistent error handling and error messages on unknown or missing subcommand, and it will also make possible for our Bash completion script to handle subcommands programmatically. The approach is guided by the following observations: - Most subcommands [1] are implemented in dedicated functions, and most of those functions [2] either have a signature matching the 'int cmd_foo(int argc, const char **argc, const char *prefix)' signature of builtin commands or can be trivially converted to that signature, because they miss only that last prefix parameter or have no parameters at all. - Subcommand arguments only have long form, and they have no double dash prefix, no negated form, and no description, and they don't take any arguments, and can't be abbreviated. - There must be exactly one subcommand among the arguments, or zero if the command has a default operation mode. - All arguments following the subcommand are considered to be arguments of the subcommand, and, conversely, arguments meant for the subcommand may not preceed the subcommand. So in the end subcommand declaration and parsing would look something like this: parse_opt_subcommand_fn *fn = NULL; struct option builtin_commit_graph_options[] = { OPT_STRING(0, "object-dir", &opts.obj_dir, N_("dir"), N_("the object directory to store the graph")), OPT_SUBCOMMAND("verify", &fn, graph_verify), OPT_SUBCOMMAND("write", &fn, graph_write), OPT_END(), }; argc = parse_options(argc, argv, prefix, options, builtin_commit_graph_usage, 0); return fn(argc, argv, prefix); Here each OPT_SUBCOMMAND specifies the name of the subcommand and the function implementing it, and the address of the same 'fn' subcommand function pointer. parse_options() then processes the arguments until it finds the first argument matching one of the subcommands, sets 'fn' to the function associated with that subcommand, and returns, leaving the rest of the arguments unprocessed. If none of the listed subcommands is found among the arguments, parse_options() will show usage and abort. If a command has a default operation mode, 'fn' should be initialized to the function implementing that mode, and parse_options() should be invoked with the PARSE_OPT_SUBCOMMAND_OPTIONAL flag. In this case parse_options() won't error out when not finding any subcommands, but will return leaving 'fn' unchanged. Note that if that default operation mode has any --options, then the PARSE_OPT_KEEP_UNKNOWN_OPT flag is necessary as well (otherwise parse_options() would error out upon seeing the unknown option meant to the default operation mode). Some thoughts about the implementation: - The same pointer to 'fn' must be specified as 'value' for each OPT_SUBCOMMAND, because there can be only one set of mutually exclusive subcommands; parse_options() will BUG() otherwise. There are other ways to tell parse_options() where to put the function associated with the subcommand given on the command line, but I didn't like them: - Change parse_options()'s signature by adding a pointer to subcommand function to be set to the function associated with the given subcommand, affecting all callsites, even those that don't have subcommands. - Introduce a specific parse_options_and_subcommand() variant with that extra funcion parameter. - I decided against automatically calling the subcommand function from within parse_options(), because: - There are commands that have to perform additional actions after option parsing but before calling the function implementing the specified subcommand. - The return code of the subcommand is usually the return code of the git command, but preserving the return code of the automatically called subcommand function would have made the API awkward. - Also add a OPT_SUBCOMMAND_F() variant to allow specifying an option flag: we have two subcommands that are purposefully excluded from completion ('git remote rm' and 'git stash save'), so they'll have to be specified with the PARSE_OPT_NOCOMPLETE flag. - Some of the 'parse_opt_flags' don't make sense with subcommands, and using them is probably just an oversight or misunderstanding. Therefore parse_options() will BUG() when invoked with any of the following flags while the options array contains at least one OPT_SUBCOMMAND: - PARSE_OPT_KEEP_DASHDASH: parse_options() stops parsing arguments when encountering a "--" argument, so it doesn't make sense to expect and keep one before a subcommand, because it would prevent the parsing of the subcommand. However, this flag is allowed in combination with the PARSE_OPT_SUBCOMMAND_OPTIONAL flag, because the double dash might be meaningful for the command's default operation mode, e.g. to disambiguate refs and pathspecs. - PARSE_OPT_STOP_AT_NON_OPTION: As its name suggests, this flag tells parse_options() to stop as soon as it encouners a non-option argument, but subcommands are by definition not options... so how could they be parsed, then?! - PARSE_OPT_KEEP_UNKNOWN: This flag can be used to collect any unknown --options and then pass them to a different command or subsystem. Surely if a command has subcommands, then this functionality should rather be delegated to one of those subcommands, and not performed by the command itself. However, this flag is allowed in combination with the PARSE_OPT_SUBCOMMAND_OPTIONAL flag, making possible to pass --options to the default operation mode. - If the command with subcommands has a default operation mode, then all arguments to the command must preceed the arguments of the subcommand. AFAICT we don't have any commands where this makes a difference, because in those commands either only the command accepts any arguments ('notes' and 'remote'), or only the default subcommand ('reflog' and 'stash'), but never both. - The 'argv' array passed to subcommand functions currently starts with the name of the subcommand. Keep this behavior. AFAICT no subcommand functions depend on the actual content of 'argv[0]', but the parse_options() call handling their options expects that the options start at argv[1]. - To support handling subcommands programmatically in our Bash completion script, 'git cmd --git-completion-helper' will now list both subcommands and regular --options, if any. This means that the completion script will have to separate subcommands (i.e. words without a double dash prefix) from --options on its own, but that's rather easy to do, and it's not much work either, because the number of subcommands a command might have is rather low, and those commands accept only a single --option or none at all. An alternative would be to introduce a separate option that lists only subcommands, but then the completion script would need not one but two git invocations and command substitutions for commands with subcommands. Note that this change doesn't affect the behavior of our Bash completion script, because when completing the --option of a command with subcommands, e.g. for 'git notes --<TAB>', then all subcommands will be filtered out anyway, as none of them will match the word to be completed starting with that double dash prefix. [1] Except 'git rerere', because many of its subcommands are implemented in the bodies of the if-else if statements parsing the command's subcommand argument. [2] Except 'credential', 'credential-store' and 'fsmonitor--daemon', because some of the functions implementing their subcommands take special parameters. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc9f988 commit fa83cc8

File tree

9 files changed

+419
-9
lines changed

9 files changed

+419
-9
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Basics
88
------
99

1010
The argument vector `argv[]` may usually contain mandatory or optional
11-
'non-option arguments', e.g. a filename or a branch, and 'options'.
11+
'non-option arguments', e.g. a filename or a branch, 'options', and
12+
'subcommands'.
1213
Options are optional arguments that start with a dash and
1314
that allow to change the behavior of a command.
1415

@@ -48,6 +49,33 @@ The parse-options API allows:
4849
option, e.g. `-a -b --option -- --this-is-a-file` indicates that
4950
`--this-is-a-file` must not be processed as an option.
5051

52+
Subcommands are special in a couple of ways:
53+
54+
* Subcommands only have long form, and they have no double dash prefix, no
55+
negated form, and no description, and they don't take any arguments, and
56+
can't be abbreviated.
57+
58+
* There must be exactly one subcommand among the arguments, or zero if the
59+
command has a default operation mode.
60+
61+
* All arguments following the subcommand are considered to be arguments of
62+
the subcommand, and, conversely, arguments meant for the subcommand may
63+
not preceed the subcommand.
64+
65+
Therefore, if the options array contains at least one subcommand and
66+
`parse_options()` encounters the first dashless argument, it will either:
67+
68+
* stop and return, if that dashless argument is a known subcommand, setting
69+
`value` to the function pointer associated with that subcommand, storing
70+
the name of the subcommand in argv[0], and leaving the rest of the
71+
arguments unprocessed, or
72+
73+
* stop and return, if it was invoked with the `PARSE_OPT_SUBCOMMAND_OPTIONAL`
74+
flag and that dashless argument doesn't match any subcommands, leaving
75+
`value` unchanged and the rest of the arguments unprocessed, or
76+
77+
* show error and usage, and abort.
78+
5179
Steps to parse options
5280
----------------------
5381

@@ -110,6 +138,13 @@ Flags are the bitwise-or of:
110138
turns it off and allows one to add custom handlers for these
111139
options, or to just leave them unknown.
112140

141+
`PARSE_OPT_SUBCOMMAND_OPTIONAL`::
142+
Don't error out when no subcommand is specified.
143+
144+
Note that `PARSE_OPT_STOP_AT_NON_OPTION` is incompatible with subcommands;
145+
while `PARSE_OPT_KEEP_DASHDASH` and `PARSE_OPT_KEEP_UNKNOWN_OPT` can only be
146+
used with subcommands when combined with `PARSE_OPT_SUBCOMMAND_OPTIONAL`.
147+
113148
Data Structure
114149
--------------
115150

@@ -241,7 +276,11 @@ There are some macros to easily define options:
241276
can be given by the user. `int_var` is set to `enum_val` when the
242277
option is used, but an error is reported if other "operating mode"
243278
option has already set its value to the same `int_var`.
279+
In new commands consider using subcommands instead.
244280

281+
`OPT_SUBCOMMAND(long, &fn_ptr, subcommand_fn)`::
282+
Define a subcommand. `subcommand_fn` is put into `fn_ptr` when
283+
this subcommand is used.
245284

246285
The last element of the array must be `OPT_END()`.
247286

builtin/blame.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
920920
break;
921921
case PARSE_OPT_HELP:
922922
case PARSE_OPT_ERROR:
923+
case PARSE_OPT_SUBCOMMAND:
923924
exit(129);
924925
case PARSE_OPT_COMPLETE:
925926
exit(0);

builtin/shortlog.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
381381
break;
382382
case PARSE_OPT_HELP:
383383
case PARSE_OPT_ERROR:
384+
case PARSE_OPT_SUBCOMMAND:
384385
exit(129);
385386
case PARSE_OPT_COMPLETE:
386387
exit(0);

parse-options.c

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ static enum parse_opt_result parse_long_opt(
324324
const char *rest, *long_name = options->long_name;
325325
enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG;
326326

327+
if (options->type == OPTION_SUBCOMMAND)
328+
continue;
327329
if (!long_name)
328330
continue;
329331

@@ -419,6 +421,19 @@ static enum parse_opt_result parse_nodash_opt(struct parse_opt_ctx_t *p,
419421
return PARSE_OPT_ERROR;
420422
}
421423

424+
static enum parse_opt_result parse_subcommand(const char *arg,
425+
const struct option *options)
426+
{
427+
for (; options->type != OPTION_END; options++)
428+
if (options->type == OPTION_SUBCOMMAND &&
429+
!strcmp(options->long_name, arg)) {
430+
*(parse_opt_subcommand_fn **)options->value = options->subcommand_fn;
431+
return PARSE_OPT_SUBCOMMAND;
432+
}
433+
434+
return PARSE_OPT_UNKNOWN;
435+
}
436+
422437
static void check_typos(const char *arg, const struct option *options)
423438
{
424439
if (strlen(arg) < 3)
@@ -442,6 +457,7 @@ static void check_typos(const char *arg, const struct option *options)
442457
static void parse_options_check(const struct option *opts)
443458
{
444459
char short_opts[128];
460+
void *subcommand_value = NULL;
445461

446462
memset(short_opts, '\0', sizeof(short_opts));
447463
for (; opts->type != OPTION_END; opts++) {
@@ -489,6 +505,14 @@ static void parse_options_check(const struct option *opts)
489505
"Are you using parse_options_step() directly?\n"
490506
"That case is not supported yet.");
491507
break;
508+
case OPTION_SUBCOMMAND:
509+
if (!opts->value || !opts->subcommand_fn)
510+
optbug(opts, "OPTION_SUBCOMMAND needs a value and a subcommand function");
511+
if (!subcommand_value)
512+
subcommand_value = opts->value;
513+
else if (subcommand_value != opts->value)
514+
optbug(opts, "all OPTION_SUBCOMMANDs need the same value");
515+
break;
492516
default:
493517
; /* ok. (usually accepts an argument) */
494518
}
@@ -499,6 +523,14 @@ static void parse_options_check(const struct option *opts)
499523
BUG_if_bug("invalid 'struct option'");
500524
}
501525

526+
static int has_subcommands(const struct option *options)
527+
{
528+
for (; options->type != OPTION_END; options++)
529+
if (options->type == OPTION_SUBCOMMAND)
530+
return 1;
531+
return 0;
532+
}
533+
502534
static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
503535
int argc, const char **argv, const char *prefix,
504536
const struct option *options,
@@ -515,6 +547,19 @@ static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
515547
ctx->prefix = prefix;
516548
ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
517549
ctx->flags = flags;
550+
ctx->has_subcommands = has_subcommands(options);
551+
if (!ctx->has_subcommands && (flags & PARSE_OPT_SUBCOMMAND_OPTIONAL))
552+
BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
553+
if (ctx->has_subcommands) {
554+
if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
555+
BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
556+
if (!(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
557+
if (flags & PARSE_OPT_KEEP_UNKNOWN_OPT)
558+
BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
559+
if (flags & PARSE_OPT_KEEP_DASHDASH)
560+
BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
561+
}
562+
}
518563
if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
519564
(flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
520565
!(flags & PARSE_OPT_ONE_SHOT))
@@ -589,6 +634,7 @@ static int show_gitcomp(const struct option *opts, int show_all)
589634
int nr_noopts = 0;
590635

591636
for (; opts->type != OPTION_END; opts++) {
637+
const char *prefix = "--";
592638
const char *suffix = "";
593639

594640
if (!opts->long_name)
@@ -598,6 +644,9 @@ static int show_gitcomp(const struct option *opts, int show_all)
598644
continue;
599645

600646
switch (opts->type) {
647+
case OPTION_SUBCOMMAND:
648+
prefix = "";
649+
break;
601650
case OPTION_GROUP:
602651
continue;
603652
case OPTION_STRING:
@@ -620,8 +669,8 @@ static int show_gitcomp(const struct option *opts, int show_all)
620669
suffix = "=";
621670
if (starts_with(opts->long_name, "no-"))
622671
nr_noopts++;
623-
printf("%s--%s%s", opts == original_opts ? "" : " ",
624-
opts->long_name, suffix);
672+
printf("%s%s%s%s", opts == original_opts ? "" : " ",
673+
prefix, opts->long_name, suffix);
625674
}
626675
show_negated_gitcomp(original_opts, show_all, -1);
627676
show_negated_gitcomp(original_opts, show_all, nr_noopts);
@@ -744,10 +793,38 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
744793
if (*arg != '-' || !arg[1]) {
745794
if (parse_nodash_opt(ctx, arg, options) == 0)
746795
continue;
747-
if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
748-
return PARSE_OPT_NON_OPTION;
749-
ctx->out[ctx->cpidx++] = ctx->argv[0];
750-
continue;
796+
if (!ctx->has_subcommands) {
797+
if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
798+
return PARSE_OPT_NON_OPTION;
799+
ctx->out[ctx->cpidx++] = ctx->argv[0];
800+
continue;
801+
}
802+
switch (parse_subcommand(arg, options)) {
803+
case PARSE_OPT_SUBCOMMAND:
804+
return PARSE_OPT_SUBCOMMAND;
805+
case PARSE_OPT_UNKNOWN:
806+
if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
807+
/*
808+
* arg is neither a short or long
809+
* option nor a subcommand. Since
810+
* this command has a default
811+
* operation mode, we have to treat
812+
* this arg and all remaining args
813+
* as args meant to that default
814+
* operation mode.
815+
* So we are done parsing.
816+
*/
817+
return PARSE_OPT_DONE;
818+
error(_("unknown subcommand: `%s'"), arg);
819+
usage_with_options(usagestr, options);
820+
case PARSE_OPT_COMPLETE:
821+
case PARSE_OPT_HELP:
822+
case PARSE_OPT_ERROR:
823+
case PARSE_OPT_DONE:
824+
case PARSE_OPT_NON_OPTION:
825+
/* Impossible. */
826+
BUG("parse_subcommand() cannot return these");
827+
}
751828
}
752829

753830
/* lone -h asks for help */
@@ -775,6 +852,7 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
775852
goto show_usage;
776853
goto unknown;
777854
case PARSE_OPT_NON_OPTION:
855+
case PARSE_OPT_SUBCOMMAND:
778856
case PARSE_OPT_HELP:
779857
case PARSE_OPT_COMPLETE:
780858
BUG("parse_short_opt() cannot return these");
@@ -800,6 +878,7 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
800878
*(char *)ctx->argv[0] = '-';
801879
goto unknown;
802880
case PARSE_OPT_NON_OPTION:
881+
case PARSE_OPT_SUBCOMMAND:
803882
case PARSE_OPT_COMPLETE:
804883
case PARSE_OPT_HELP:
805884
BUG("parse_short_opt() cannot return these");
@@ -831,6 +910,7 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
831910
case PARSE_OPT_HELP:
832911
goto show_usage;
833912
case PARSE_OPT_NON_OPTION:
913+
case PARSE_OPT_SUBCOMMAND:
834914
case PARSE_OPT_COMPLETE:
835915
BUG("parse_long_opt() cannot return these");
836916
case PARSE_OPT_DONE:
@@ -840,6 +920,18 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
840920
unknown:
841921
if (ctx->flags & PARSE_OPT_ONE_SHOT)
842922
break;
923+
if (ctx->has_subcommands &&
924+
(ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL) &&
925+
(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) {
926+
/*
927+
* Found an unknown option given to a command with
928+
* subcommands that has a default operation mode:
929+
* we treat this option and all remaining args as
930+
* arguments meant to that default operation mode.
931+
* So we are done parsing.
932+
*/
933+
return PARSE_OPT_DONE;
934+
}
843935
if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT))
844936
return PARSE_OPT_UNKNOWN;
845937
ctx->out[ctx->cpidx++] = ctx->argv[0];
@@ -885,7 +977,14 @@ int parse_options(int argc, const char **argv,
885977
case PARSE_OPT_COMPLETE:
886978
exit(0);
887979
case PARSE_OPT_NON_OPTION:
980+
case PARSE_OPT_SUBCOMMAND:
981+
break;
888982
case PARSE_OPT_DONE:
983+
if (ctx.has_subcommands &&
984+
!(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
985+
error(_("need a subcommand"));
986+
usage_with_options(usagestr, options);
987+
}
889988
break;
890989
case PARSE_OPT_UNKNOWN:
891990
if (ctx.argv[0][1] == '-') {
@@ -1010,6 +1109,8 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
10101109
size_t pos;
10111110
int pad;
10121111

1112+
if (opts->type == OPTION_SUBCOMMAND)
1113+
continue;
10131114
if (opts->type == OPTION_GROUP) {
10141115
fputc('\n', outfile);
10151116
need_newline = 0;

0 commit comments

Comments
 (0)