The parse-options API is used to parse and massage options in Git and to provide a usage help with consistent look.

Basics

The argument vector may usually contain mandatory or optional 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.

  • There are basically three types of options: boolean options, options with (mandatory) arguments and options with optional arguments (i.e. a boolean option that can be adjusted).

  • There are basically two forms of options: Short options consist of one dash () and one alphanumeric character. Long options begin with two dashes () and some alphanumeric characters.

  • Options are case-sensitive. Please define lower-case long options only.

The parse-options API allows:

  • stuck and separate form of options with arguments. is stuck, is separate form. is stuck, is separate form.

  • Long options may be abbreviated, as long as the abbreviation is unambiguous.

  • Short options may be bundled, e.g. can be specified as .

  • Boolean long options can be negated (or unset) by prepending , e.g. instead of . Conversely, options that begin with can be negated by removing it. Other long options can be unset (e.g., set string to NULL, set integer to 0) by prepending .

  • Options and non-option arguments can clearly be separated using the option, e.g. indicates that 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 precede the subcommand.

Therefore, if the options array contains at least one subcommand and encounters the first dashless argument, it will either:

  • stop and return, if that dashless argument is a known subcommand, setting 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 flag and that dashless argument doesn’t match any subcommands, leaving unchanged and the rest of the arguments unprocessed, or

  • show error and usage, and abort.

Steps to parse options

  1. define a NULL-terminated array containing alternative usage strings

  2. define array as described below in section Data Structure.

  3. in call

    argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);

    will filter out the processed options of and leave the non-option arguments in . is updated appropriately because of the assignment.

    You can also pass NULL instead of a usage array as the fifth parameter of parse_options(), to avoid displaying a help screen with usage info and option list. This should only be done if necessary, e.g. to implement a limited parser for only a subset of the options that needs to be run before the full parser, which in turn shows the full help message.

    Flags are the bitwise-or of:

    Keep the that usually separates options from non-option arguments.

    Usually the whole argument vector is massaged and reordered. Using this flag, processing is stopped at the first non-option argument.

    Keep the first argument, which contains the program name. It’s removed from argv[] by default.

    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 takes a value (which we can’t know), the second one is mistakenly interpreted as a known option. Similarly, if is set, the second argument in will be mistakenly interpreted as a 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.

    By default, parse_options() handles , and internally, by showing a help screen. This option turns it off and allows one to add custom handlers for these options, or to just leave them unknown.

    Don’t error out when no subcommand is specified.

Note that is incompatible with subcommands; while and can only be used with subcommands when combined with .

Data Structure

The main data structure is an array of the struct, say . There are some macros to easily define options:

Add .

Add and .

Add .

Add .

Add .

Add .

Start an option group. is a short string that describes the group or an empty string. Start the description with an upper-case letter.

Introduce a boolean option. is set to one with and set to zero with .

Introduce a count-up option. Each use of increments , starting from zero (even if initially negative), and resets it to zero. To determine if or was encountered at all, initialize to a negative value, and if it is still negative after parse_options(), then neither nor was seen.

Introduce a boolean option. If used, is bitwise-ored with .

Introduce a boolean option. If used, is bitwise-anded with the inverted .

Introduce an integer option. is set to with , and reset to zero with .

Introduce an option with string argument. The string argument is put into .

Introduce an option with string argument. The string argument is stored as an element in . Use of will clear the list of preceding values.

Introduce an option with integer argument. The integer is put into .

Introduce an option with a size argument. The argument must be a non-negative integer and may include a suffix of k, m or g to scale the provided value by 1024, 10242 or 10243 respectively. The scaled value is put into .

Introduce an option with expiry date argument, see . The timestamp is put into .

Introduce an option with argument. The argument will be fed into the function given by and the result will be put into . See Option Callbacks below for a more elaborate description.

Introduce an option with a filename argument. The filename will be prefixed by passing the filename along with the prefix argument of to .

Recognize numerical options like -123 and feed the integer as if it was an argument to the function given by . The result will be put into . There can be only one such option definition. It cannot be negated and it takes no arguments. Short options that happen to be digits take precedence over it.

Introduce an option that takes an optional argument that can have one of three values: "always", "never", or "auto". If the argument is not given, it defaults to "always". The form works like ; it cannot take an argument. If "always", set to 1; if "never", set to 0; if "auto", set to 1 if stdout is a tty or a pager, 0 otherwise.

Introduce an option that has no effect and takes no arguments. Use it to hide deprecated options that are still to be recognized and ignored silently.

Introduce an option that will be reconstructed into a char* string, which must be initialized to NULL. This is useful when you need to pass the command-line option to another command. Any previous value will be overwritten, so this should only be used for options where the last one specified on the command line wins.

Introduce an option where all instances of it on the command-line will be reconstructed into a strvec. This is useful when you need to pass the command-line option, which can be specified multiple times, to another command.

Define an "operation mode" option, only one of which in the same group of "operating mode" options that share the same can be given by the user. is set to when the option is used, but an error is reported if other "operating mode" option has already set its value to the same . In new commands consider using subcommands instead.

Define a subcommand. is put into when this subcommand is used.

The last element of the array must be .

If not stated otherwise, interpret the arguments as follows:

  • is a character for the short option (e.g. for , use to omit),

  • is a string for the long option (e.g. for , use to omit),

  • is an integer variable,

  • is a string variable (),

  • is the string that is shown as argument (e.g. will result in ). If set to , three dots () will be displayed.

  • is a short string to describe the effect of the option. It shall begin with a lower-case letter and a full stop () shall be omitted at the end.

Option Callbacks

The function must be defined in this form:

int func(const struct option *opt, const char *arg, int unset)

The callback mechanism is as follows:

  • Inside , the only interesting member of the structure given by is the void pointer . will be the value that is saved into , if you use . For example, do to get 42 into an variable.

  • Return value indicates success and non-zero return value will invoke and, thus, die.

  • If the user negates the option, is and is 1.

Sophisticated option parsing

If you need, for example, option callbacks with optional arguments or without arguments at all, or if you need other special cases, that are not handled by the macros above, you need to specify the members of the structure manually.

This is not covered in this document, but well documented in itself.

Examples

See and , , , , , for real-world examples.