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

@ -8,7 +8,8 @@ Basics
------
The argument vector `argv[]` may usually contain mandatory or optional
'non-option arguments', e.g. a filename or a branch, and 'options'.
'non-option arguments', e.g. a filename or a branch, 'options', and
'subcommands'.
Options are optional arguments that start with a dash and
that allow to change the behavior of a command.
@ -48,6 +49,33 @@ The parse-options API allows:
option, e.g. `-a -b --option -- --this-is-a-file` indicates that
`--this-is-a-file` must not be processed as an option.
Subcommands are special in a couple of ways:
* Subcommands 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.
Therefore, if the options array contains at least one subcommand and
`parse_options()` encounters the first dashless argument, it will either:
* stop and return, if that dashless argument is a known subcommand, setting
`value` to the function pointer associated with that subcommand, storing
the name of the subcommand in argv[0], and leaving the rest of the
arguments unprocessed, or
* stop and return, if it was invoked with the `PARSE_OPT_SUBCOMMAND_OPTIONAL`
flag and that dashless argument doesn't match any subcommands, leaving
`value` unchanged and the rest of the arguments unprocessed, or
* show error and usage, and abort.
Steps to parse options
----------------------
@ -90,8 +118,8 @@ Flags are the bitwise-or of:
Keep the first argument, which contains the program name. It's
removed from argv[] by default.
`PARSE_OPT_KEEP_UNKNOWN`::
Keep unknown arguments instead of erroring out. This doesn't
`PARSE_OPT_KEEP_UNKNOWN_OPT`::
Keep unknown options instead of erroring out. This doesn't
work for all combinations of arguments as users might expect
it to do. E.g. if the first argument in `--unknown --known`
takes a value (which we can't know), the second one is
@ -101,6 +129,8 @@ Flags are the bitwise-or of:
non-option, not as a value belonging to the unknown option,
the parser early. That's why parse_options() errors out if
both options are set.
Note that non-option arguments are always kept, even without
this flag.
`PARSE_OPT_NO_INTERNAL_HELP`::
By default, parse_options() handles `-h`, `--help` and
@ -108,6 +138,13 @@ Flags are the bitwise-or of:
turns it off and allows one to add custom handlers for these
options, or to just leave them unknown.
`PARSE_OPT_SUBCOMMAND_OPTIONAL`::
Don't error out when no subcommand is specified.
Note that `PARSE_OPT_STOP_AT_NON_OPTION` is incompatible with subcommands;
while `PARSE_OPT_KEEP_DASHDASH` and `PARSE_OPT_KEEP_UNKNOWN_OPT` can only be
used with subcommands when combined with `PARSE_OPT_SUBCOMMAND_OPTIONAL`.
Data Structure
--------------
@ -236,10 +273,14 @@ There are some macros to easily define options:
`OPT_CMDMODE(short, long, &int_var, description, enum_val)`::
Define an "operation mode" option, only one of which in the same
group of "operating mode" options that share the same `int_var`
can be given by the user. `enum_val` is set to `int_var` when the
can be given by the user. `int_var` is set to `enum_val` when the
option is used, but an error is reported if other "operating mode"
option has already set its value to the same `int_var`.
In new commands consider using subcommands instead.
`OPT_SUBCOMMAND(long, &fn_ptr, subcommand_fn)`::
Define a subcommand. `subcommand_fn` is put into `fn_ptr` when
this subcommand is used.
The last element of the array must be `OPT_END()`.