blob: 1317db4d6ca798c0ffa5150a7373c37b5599ecd9 [file] [log] [blame]
Junio C Hamano3dac5042007-12-15 08:40:541parse-options API
2=================
3
Junio C Hamano076ffcc2013-02-06 05:13:214The parse-options API is used to parse and massage options in Git
Junio C Hamano4224f992008-06-23 07:14:085and to provide a usage help with consistent look.
Junio C Hamano3dac5042007-12-15 08:40:546
Junio C Hamano4224f992008-06-23 07:14:087Basics
8------
9
10The argument vector `argv[]` may usually contain mandatory or optional
11'non-option arguments', e.g. a filename or a branch, and 'options'.
12Options are optional arguments that start with a dash and
13that allow to change the behavior of a command.
14
15* There are basically three types of options:
16 'boolean' options,
17 options with (mandatory) 'arguments' and
18 options with 'optional arguments'
19 (i.e. a boolean option that can be adjusted).
20
21* There are basically two forms of options:
22 'Short options' consist of one dash (`-`) and one alphanumeric
23 character.
Junio C Hamanob76a6862012-05-02 22:02:4624 'Long options' begin with two dashes (`--`) and some
Junio C Hamano4224f992008-06-23 07:14:0825 alphanumeric characters.
26
27* Options are case-sensitive.
28 Please define 'lower-case long options' only.
29
30The parse-options API allows:
31
32* 'sticked' and 'separate form' of options with arguments.
33 `-oArg` is sticked, `-o Arg` is separate form.
Junio C Hamanob76a6862012-05-02 22:02:4634 `--option=Arg` is sticked, `--option Arg` is separate form.
Junio C Hamano4224f992008-06-23 07:14:0835
36* Long options may be 'abbreviated', as long as the abbreviation
37 is unambiguous.
38
39* Short options may be bundled, e.g. `-a -b` can be specified as `-ab`.
40
41* Boolean long options can be 'negated' (or 'unset') by prepending
Junio C Hamanob76a6862012-05-02 22:02:4642 `no-`, e.g. `--no-abbrev` instead of `--abbrev`. Conversely,
Junio C Hamano81d540a2012-03-02 19:52:4743 options that begin with `no-` can be 'negated' by removing it.
Junio C Hamanoc7102962013-05-29 23:57:1744 Other long options can be unset (e.g., set string to NULL, set
45 integer to 0) by prepending `no-`.
Junio C Hamano4224f992008-06-23 07:14:0846
Junio C Hamanob76a6862012-05-02 22:02:4647* Options and non-option arguments can clearly be separated using the `--`
48 option, e.g. `-a -b --option -- --this-is-a-file` indicates that
49 `--this-is-a-file` must not be processed as an option.
Junio C Hamano4224f992008-06-23 07:14:0850
51Steps to parse options
52----------------------
53
54. `#include "parse-options.h"`
55
56. define a NULL-terminated
57 `static const char * const builtin_foo_usage[]` array
58 containing alternative usage strings
59
60. define `builtin_foo_options` array as described below
61 in section 'Data Structure'.
62
63. in `cmd_foo(int argc, const char **argv, const char *prefix)`
64 call
65
Junio C Hamano3d141512009-06-01 01:22:4066argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);
Junio C Hamano4224f992008-06-23 07:14:0867+
68`parse_options()` will filter out the processed options of `argv[]` and leave the
69non-option arguments in `argv[]`.
70`argc` is updated appropriately because of the assignment.
71+
Junio C Hamano3d141512009-06-01 01:22:4072You can also pass NULL instead of a usage array as the fifth parameter of
Junio C Hamano18f51402009-03-10 16:39:1373parse_options(), to avoid displaying a help screen with usage info and
74option list. This should only be done if necessary, e.g. to implement
75a limited parser for only a subset of the options that needs to be run
76before the full parser, which in turn shows the full help message.
77+
Junio C Hamano4224f992008-06-23 07:14:0878Flags are the bitwise-or of:
79
80`PARSE_OPT_KEEP_DASHDASH`::
Junio C Hamanob76a6862012-05-02 22:02:4681Keep the `--` that usually separates options from
Junio C Hamano4224f992008-06-23 07:14:0882non-option arguments.
83
84`PARSE_OPT_STOP_AT_NON_OPTION`::
85Usually the whole argument vector is massaged and reordered.
86Using this flag, processing is stopped at the first non-option
87argument.
88
Junio C Hamano18f51402009-03-10 16:39:1389`PARSE_OPT_KEEP_ARGV0`::
90Keep the first argument, which contains the program name. It's
91removed from argv[] by default.
92
93`PARSE_OPT_KEEP_UNKNOWN`::
94Keep unknown arguments instead of erroring out. This doesn't
95work for all combinations of arguments as users might expect
96it to do. E.g. if the first argument in `--unknown --known`
97takes a value (which we can't know), the second one is
98mistakenly interpreted as a known option. Similarly, if
99`PARSE_OPT_STOP_AT_NON_OPTION` is set, the second argument in
100`--unknown value` will be mistakenly interpreted as a
101non-option, not as a value belonging to the unknown option,
102the parser early. That's why parse_options() errors out if
103both options are set.
104
105`PARSE_OPT_NO_INTERNAL_HELP`::
106By default, parse_options() handles `-h`, `--help` and
107`--help-all` internally, by showing a help screen. This option
108turns it off and allows one to add custom handlers for these
109options, or to just leave them unknown.
110
Junio C Hamano4224f992008-06-23 07:14:08111Data Structure
112--------------
113
114The main data structure is an array of the `option` struct,
115say `static struct option builtin_add_options[]`.
116There are some macros to easily define options:
117
118`OPT__ABBREV(&int_var)`::
Junio C Hamanob76a6862012-05-02 22:02:46119Add `--abbrev[=<n>]`.
Junio C Hamano4224f992008-06-23 07:14:08120
Junio C Hamano4aa0bcc2010-03-03 05:13:12121`OPT__COLOR(&int_var, description)`::
Junio C Hamanob76a6862012-05-02 22:02:46122Add `--color[=<when>]` and `--no-color`.
Junio C Hamano4aa0bcc2010-03-03 05:13:12123
Junio C Hamano97bcb482010-11-25 03:16:07124`OPT__DRY_RUN(&int_var, description)`::
Junio C Hamanob76a6862012-05-02 22:02:46125Add `-n, --dry-run`.
Junio C Hamano4224f992008-06-23 07:14:08126
Junio C Hamano97bcb482010-11-25 03:16:07127`OPT__FORCE(&int_var, description)`::
Junio C Hamanob76a6862012-05-02 22:02:46128Add `-f, --force`.
Junio C Hamano97bcb482010-11-25 03:16:07129
130`OPT__QUIET(&int_var, description)`::
Junio C Hamanob76a6862012-05-02 22:02:46131Add `-q, --quiet`.
Junio C Hamano4224f992008-06-23 07:14:08132
Junio C Hamano97bcb482010-11-25 03:16:07133`OPT__VERBOSE(&int_var, description)`::
Junio C Hamanob76a6862012-05-02 22:02:46134Add `-v, --verbose`.
Junio C Hamano4224f992008-06-23 07:14:08135
136`OPT_GROUP(description)`::
137Start an option group. `description` is a short string that
138describes the group or an empty string.
139Start the description with an upper-case letter.
140
Junio C Hamano4c6612f2011-10-12 23:54:21141`OPT_BOOL(short, long, &int_var, description)`::
142Introduce a boolean option. `int_var` is set to one with
143`--option` and set to zero with `--no-option`.
144
145`OPT_COUNTUP(short, long, &int_var, description)`::
146Introduce a count-up option.
147`int_var` is incremented on each use of `--option`, and
148reset to zero with `--no-option`.
Junio C Hamano4224f992008-06-23 07:14:08149
150`OPT_BIT(short, long, &int_var, description, mask)`::
151Introduce a boolean option.
152If used, `int_var` is bitwise-ored with `mask`.
153
Junio C Hamano7ae18252009-05-23 09:53:32154`OPT_NEGBIT(short, long, &int_var, description, mask)`::
155Introduce a boolean option.
156If used, `int_var` is bitwise-anded with the inverted `mask`.
157
Junio C Hamano4224f992008-06-23 07:14:08158`OPT_SET_INT(short, long, &int_var, description, integer)`::
Junio C Hamano4c6612f2011-10-12 23:54:21159Introduce an integer option.
160`int_var` is set to `integer` with `--option`, and
161reset to zero with `--no-option`.
Junio C Hamano4224f992008-06-23 07:14:08162
163`OPT_SET_PTR(short, long, &ptr_var, description, ptr)`::
164Introduce a boolean option.
165If used, set `ptr_var` to `ptr`.
166
167`OPT_STRING(short, long, &str_var, arg_str, description)`::
168Introduce an option with string argument.
169The string argument is put into `str_var`.
170
171`OPT_INTEGER(short, long, &int_var, description)`::
172Introduce an option with integer argument.
173The integer is put into `int_var`.
174
175`OPT_DATE(short, long, &int_var, description)`::
176Introduce an option with date argument, see `approxidate()`.
177The timestamp is put into `int_var`.
178
Junio C Hamanoc7102962013-05-29 23:57:17179`OPT_EXPIRY_DATE(short, long, &int_var, description)`::
180Introduce an option with expiry date argument, see `parse_expiry_date()`.
181The timestamp is put into `int_var`.
182
Junio C Hamano4224f992008-06-23 07:14:08183`OPT_CALLBACK(short, long, &var, arg_str, description, func_ptr)`::
184Introduce an option with argument.
185The argument will be fed into the function given by `func_ptr`
186and the result will be put into `var`.
187See 'Option Callbacks' below for a more elaborate description.
188
Junio C Hamano3d141512009-06-01 01:22:40189`OPT_FILENAME(short, long, &var, description)`::
190Introduce an option with a filename argument.
191The filename will be prefixed by passing the filename along with
192the prefix argument of `parse_options()` to `prefix_filename()`.
193
Junio C Hamano4224f992008-06-23 07:14:08194`OPT_ARGUMENT(long, description)`::
195Introduce a long-option argument that will be kept in `argv[]`.
196
Junio C Hamano7ae18252009-05-23 09:53:32197`OPT_NUMBER_CALLBACK(&var, description, func_ptr)`::
198Recognize numerical options like -123 and feed the integer as
199if it was an argument to the function given by `func_ptr`.
200The result will be put into `var`. There can be only one such
201option definition. It cannot be negated and it takes no
202arguments. Short options that happen to be digits take
203precedence over it.
204
Junio C Hamano4aa0bcc2010-03-03 05:13:12205`OPT_COLOR_FLAG(short, long, &int_var, description)`::
206Introduce an option that takes an optional argument that can
207have one of three values: "always", "never", or "auto". If the
208argument is not given, it defaults to "always". The `--no-` form
209works like `--long=never`; it cannot take an argument. If
210"always", set `int_var` to 1; if "never", set `int_var` to 0; if
211"auto", set `int_var` to 1 if stdout is a tty or a pager,
2120 otherwise.
213
Junio C Hamano4c6612f2011-10-12 23:54:21214`OPT_NOOP_NOARG(short, long)`::
215Introduce an option that has no effect and takes no arguments.
216Use it to hide deprecated options that are still to be recognized
217and ignored silently.
218
Junio C Hamano4224f992008-06-23 07:14:08219
220The last element of the array must be `OPT_END()`.
221
222If not stated otherwise, interpret the arguments as follows:
223
224* `short` is a character for the short option
Junio C Hamanob76a6862012-05-02 22:02:46225 (e.g. `'e'` for `-e`, use `0` to omit),
Junio C Hamano4224f992008-06-23 07:14:08226
227* `long` is a string for the long option
Junio C Hamanob76a6862012-05-02 22:02:46228 (e.g. `"example"` for `--example`, use `NULL` to omit),
Junio C Hamano4224f992008-06-23 07:14:08229
230* `int_var` is an integer variable,
231
232* `str_var` is a string variable (`char *`),
233
234* `arg_str` is the string that is shown as argument
235 (e.g. `"branch"` will result in `<branch>`).
236 If set to `NULL`, three dots (`...`) will be displayed.
237
238* `description` is a short string to describe the effect of the option.
239 It shall begin with a lower-case letter and a full stop (`.`) shall be
240 omitted at the end.
241
242Option Callbacks
243----------------
244
245The function must be defined in this form:
246
247int func(const struct option *opt, const char *arg, int unset)
248
249The callback mechanism is as follows:
250
Junio C Hamanod4f35a92009-05-17 05:39:29251* Inside `func`, the only interesting member of the structure
Junio C Hamanob76a6862012-05-02 22:02:46252 given by `opt` is the void pointer `opt->value`.
253 `*opt->value` will be the value that is saved into `var`, if you
Junio C Hamano4224f992008-06-23 07:14:08254 use `OPT_CALLBACK()`.
Junio C Hamanob76a6862012-05-02 22:02:46255 For example, do `*(unsigned long *)opt->value = 42;` to get 42
Junio C Hamano4224f992008-06-23 07:14:08256 into an `unsigned long` variable.
257
258* Return value `0` indicates success and non-zero return
259 value will invoke `usage_with_options()` and, thus, die.
260
261* If the user negates the option, `arg` is `NULL` and `unset` is 1.
262
263Sophisticated option parsing
264----------------------------
265
266If you need, for example, option callbacks with optional arguments
267or without arguments at all, or if you need other special cases,
268that are not handled by the macros above, you need to specify the
269members of the `option` structure manually.
270
271This is not covered in this document, but well documented
272in `parse-options.h` itself.
273
274Examples
275--------
276
277See `test-parse-options.c` and
278`builtin-add.c`,
279`builtin-clone.c`,
280`builtin-commit.c`,
281`builtin-fetch.c`,
282`builtin-fsck.c`,
283`builtin-rm.c`
284for real-world examples.