t8001/t8002: blame: add tests of -L line numbers less than 1
[git.git] / Documentation / technical / api-parse-options.txt
blob0be2b5159f1b349f0c263eb9e8ba696e2a57ebc0
1 parse-options API
2 =================
4 The parse-options API is used to parse and massage options in Git
5 and to provide a usage help with consistent look.
7 Basics
8 ------
10 The argument vector `argv[]` may usually contain mandatory or optional
11 'non-option arguments', e.g. a filename or a branch, and 'options'.
12 Options are optional arguments that start with a dash and
13 that allow to change the behavior of a command.
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).
21 * There are basically two forms of options:
22   'Short options' consist of one dash (`-`) and one alphanumeric
23   character.
24   'Long options' begin with two dashes (`--`) and some
25   alphanumeric characters.
27 * Options are case-sensitive.
28   Please define 'lower-case long options' only.
30 The parse-options API allows:
32 * 'sticked' and 'separate form' of options with arguments.
33   `-oArg` is sticked, `-o Arg` is separate form.
34   `--option=Arg` is sticked, `--option Arg` is separate form.
36 * Long options may be 'abbreviated', as long as the abbreviation
37   is unambiguous.
39 * Short options may be bundled, e.g. `-a -b` can be specified as `-ab`.
41 * Boolean long options can be 'negated' (or 'unset') by prepending
42   `no-`, e.g. `--no-abbrev` instead of `--abbrev`. Conversely,
43   options that begin with `no-` can be 'negated' by removing it.
44   Other long options can be unset (e.g., set string to NULL, set
45   integer to 0) by prepending `no-`.
47 * 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.
51 Steps to parse options
52 ----------------------
54 . `#include "parse-options.h"`
56 . define a NULL-terminated
57   `static const char * const builtin_foo_usage[]` array
58   containing alternative usage strings
60 . define `builtin_foo_options` array as described below
61   in section 'Data Structure'.
63 . in `cmd_foo(int argc, const char **argv, const char *prefix)`
64   call
66         argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);
68 `parse_options()` will filter out the processed options of `argv[]` and leave the
69 non-option arguments in `argv[]`.
70 `argc` is updated appropriately because of the assignment.
72 You can also pass NULL instead of a usage array as the fifth parameter of
73 parse_options(), to avoid displaying a help screen with usage info and
74 option list.  This should only be done if necessary, e.g. to implement
75 a limited parser for only a subset of the options that needs to be run
76 before the full parser, which in turn shows the full help message.
78 Flags are the bitwise-or of:
80 `PARSE_OPT_KEEP_DASHDASH`::
81         Keep the `--` that usually separates options from
82         non-option arguments.
84 `PARSE_OPT_STOP_AT_NON_OPTION`::
85         Usually the whole argument vector is massaged and reordered.
86         Using this flag, processing is stopped at the first non-option
87         argument.
89 `PARSE_OPT_KEEP_ARGV0`::
90         Keep the first argument, which contains the program name.  It's
91         removed from argv[] by default.
93 `PARSE_OPT_KEEP_UNKNOWN`::
94         Keep unknown arguments instead of erroring out.  This doesn't
95         work for all combinations of arguments as users might expect
96         it to do.  E.g. if the first argument in `--unknown --known`
97         takes a value (which we can't know), the second one is
98         mistakenly 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
101         non-option, not as a value belonging to the unknown option,
102         the parser early.  That's why parse_options() errors out if
103         both options are set.
105 `PARSE_OPT_NO_INTERNAL_HELP`::
106         By default, parse_options() handles `-h`, `--help` and
107         `--help-all` internally, by showing a help screen.  This option
108         turns it off and allows one to add custom handlers for these
109         options, or to just leave them unknown.
111 Data Structure
112 --------------
114 The main data structure is an array of the `option` struct,
115 say `static struct option builtin_add_options[]`.
116 There are some macros to easily define options:
118 `OPT__ABBREV(&int_var)`::
119         Add `--abbrev[=<n>]`.
121 `OPT__COLOR(&int_var, description)`::
122         Add `--color[=<when>]` and `--no-color`.
124 `OPT__DRY_RUN(&int_var, description)`::
125         Add `-n, --dry-run`.
127 `OPT__FORCE(&int_var, description)`::
128         Add `-f, --force`.
130 `OPT__QUIET(&int_var, description)`::
131         Add `-q, --quiet`.
133 `OPT__VERBOSE(&int_var, description)`::
134         Add `-v, --verbose`.
136 `OPT_GROUP(description)`::
137         Start an option group. `description` is a short string that
138         describes the group or an empty string.
139         Start the description with an upper-case letter.
141 `OPT_BOOL(short, long, &int_var, description)`::
142         Introduce a boolean option. `int_var` is set to one with
143         `--option` and set to zero with `--no-option`.
145 `OPT_COUNTUP(short, long, &int_var, description)`::
146         Introduce a count-up option.
147         `int_var` is incremented on each use of `--option`, and
148         reset to zero with `--no-option`.
150 `OPT_BIT(short, long, &int_var, description, mask)`::
151         Introduce a boolean option.
152         If used, `int_var` is bitwise-ored with `mask`.
154 `OPT_NEGBIT(short, long, &int_var, description, mask)`::
155         Introduce a boolean option.
156         If used, `int_var` is bitwise-anded with the inverted `mask`.
158 `OPT_SET_INT(short, long, &int_var, description, integer)`::
159         Introduce an integer option.
160         `int_var` is set to `integer` with `--option`, and
161         reset to zero with `--no-option`.
163 `OPT_SET_PTR(short, long, &ptr_var, description, ptr)`::
164         Introduce a boolean option.
165         If used, set `ptr_var` to `ptr`.
167 `OPT_STRING(short, long, &str_var, arg_str, description)`::
168         Introduce an option with string argument.
169         The string argument is put into `str_var`.
171 `OPT_INTEGER(short, long, &int_var, description)`::
172         Introduce an option with integer argument.
173         The integer is put into `int_var`.
175 `OPT_DATE(short, long, &int_var, description)`::
176         Introduce an option with date argument, see `approxidate()`.
177         The timestamp is put into `int_var`.
179 `OPT_EXPIRY_DATE(short, long, &int_var, description)`::
180         Introduce an option with expiry date argument, see `parse_expiry_date()`.
181         The timestamp is put into `int_var`.
183 `OPT_CALLBACK(short, long, &var, arg_str, description, func_ptr)`::
184         Introduce an option with argument.
185         The argument will be fed into the function given by `func_ptr`
186         and the result will be put into `var`.
187         See 'Option Callbacks' below for a more elaborate description.
189 `OPT_FILENAME(short, long, &var, description)`::
190         Introduce an option with a filename argument.
191         The filename will be prefixed by passing the filename along with
192         the prefix argument of `parse_options()` to `prefix_filename()`.
194 `OPT_ARGUMENT(long, description)`::
195         Introduce a long-option argument that will be kept in `argv[]`.
197 `OPT_NUMBER_CALLBACK(&var, description, func_ptr)`::
198         Recognize numerical options like -123 and feed the integer as
199         if it was an argument to the function given by `func_ptr`.
200         The result will be put into `var`.  There can be only one such
201         option definition.  It cannot be negated and it takes no
202         arguments.  Short options that happen to be digits take
203         precedence over it.
205 `OPT_COLOR_FLAG(short, long, &int_var, description)`::
206         Introduce an option that takes an optional argument that can
207         have one of three values: "always", "never", or "auto".  If the
208         argument is not given, it defaults to "always".  The `--no-` form
209         works 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,
212         0 otherwise.
214 `OPT_NOOP_NOARG(short, long)`::
215         Introduce an option that has no effect and takes no arguments.
216         Use it to hide deprecated options that are still to be recognized
217         and ignored silently.
220 The last element of the array must be `OPT_END()`.
222 If not stated otherwise, interpret the arguments as follows:
224 * `short` is a character for the short option
225   (e.g. `'e'` for `-e`, use `0` to omit),
227 * `long` is a string for the long option
228   (e.g. `"example"` for `--example`, use `NULL` to omit),
230 * `int_var` is an integer variable,
232 * `str_var` is a string variable (`char *`),
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.
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.
242 Option Callbacks
243 ----------------
245 The function must be defined in this form:
247         int func(const struct option *opt, const char *arg, int unset)
249 The callback mechanism is as follows:
251 * Inside `func`, the only interesting member of the structure
252   given by `opt` is the void pointer `opt->value`.
253   `*opt->value` will be the value that is saved into `var`, if you
254   use `OPT_CALLBACK()`.
255   For example, do `*(unsigned long *)opt->value = 42;` to get 42
256   into an `unsigned long` variable.
258 * Return value `0` indicates success and non-zero return
259   value will invoke `usage_with_options()` and, thus, die.
261 * If the user negates the option, `arg` is `NULL` and `unset` is 1.
263 Sophisticated option parsing
264 ----------------------------
266 If you need, for example, option callbacks with optional arguments
267 or without arguments at all, or if you need other special cases,
268 that are not handled by the macros above, you need to specify the
269 members of the `option` structure manually.
271 This is not covered in this document, but well documented
272 in `parse-options.h` itself.
274 Examples
275 --------
277 See `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`
284 for real-world examples.