1# Options
2
3## Simple options
4
5The most versatile addition to a command line program is a option. This is like a flag, but it takes an argument. CLI11 handles all the details for many types of options for you, based on their type. To add an option:
6
7```cpp
8int int_option{0};
9app.add_option("-i", int_option, "Optional description");
10```
11
12This will bind the option `-i` to the integer `int_option`. On the command line, a single value that can be converted to an integer will be expected. Non-integer results will fail. If that option is not given, CLI11 will not touch the initial value. This allows you to set up defaults by simply setting your value beforehand. If you want CLI11 to display your default value, you can add `->capture_default_str()` after the option.
13
14```cpp
15int int_option{0};
16app.add_option("-i", int_option, "Optional description")->capture_default_str();
17```
18
19You can use any C++ int-like type, not just `int`. CLI11 understands the following categories of types:
20
21| Type        | CLI11 |
22|-------------|-------|
23| number like    | Integers, floats, bools, or any type that can be constructed from an integer or floating point number.  Accepts common numerical strings like `0xFF` as well as octal, and decimal |
24| string-like | std\::string, or anything that can be constructed from or assigned a std\::string |
25| char | For a single char, single string values are accepted, otherwise longer strings are treated as integral values and a conversion is attempted |
26| complex-number | std::complex or any type which has a real(), and imag() operations available, will allow 1 or 2 string definitions like "1+2j" or two arguments "1","2" |
27| enumeration | any enum or enum class type is supported through conversion from the underlying type(typically int, though it can be specified otherwise) |
28| container-like | a container(like vector) of any available types including other containers |
29| wrapper | any other object with a `value_type` static definition where the type specified by `value_type` is one of the type in this list, including `std::atomic<>` |
30| tuple | a tuple, pair, or array, or other type with a tuple size and tuple_type operations defined and the members being a type contained in this list |
31| function | A function that takes an array of strings and returns a string that describes the conversion failure or empty for success. May be the empty function. (`{}`) |
32| streamable | any other type with a `<<` operator will also work |
33
34By default, CLI11 will assume that an option is optional, and one value is expected if you do not use a vector. You can change this on a specific option using option modifiers.  An option name may start with any character except ('-', ' ', '\n', and '!'). For long options, after the first character all characters are allowed except ('=',':','{',' ', '\n'). Names are given as a comma separated string, with the dash or dashes. An option can have as many names as you want, and afterward, using `count`, you can use any of the names, with dashes as needed, to count the options. One of the names is allowed to be given without proceeding dash(es); if present the option is a positional option, and that name will be used on the help line for its positional form.
35
36## Positional options and aliases
37
38When you give an option on the command line without a name, that is a positional option. Positional options are accepted in the same order they are defined. So, for example:
39
40```term
41gitbook:examples $ ./a.out one --two three four
42```
43
44The string `one` would have to be the first positional option. If `--two` is a flag, then the remaining two strings are positional. If `--two` is a one-argument option, then `four` is the second positional. If `--two` accepts two or more arguments, then there are no more positionals.
45
46To make a positional option, you simply give CLI11 one name that does not start with a dash. You can have as many (non-overlapping) names as you want for an option, but only one positional name. So the following name string is valid:
47
48```cpp
49"-a,-b,--alpha,--beta,mypos"
50```
51
52This would make two short option aliases, two long option alias, and the option would be also be accepted as a positional.
53
54## Containers of options
55
56If you use a vector or other container instead of a plain option, you can accept more than one value on the command line. By default, a container accepts as many options as possible, until the next value that could be a valid option name. You can specify a set number using an option modifier `->expected(N)`. (The default unlimited behavior on vectors is restored with `N=-1`) CLI11 does not differentiate between these two methods for unlimited acceptance options.
57
58| Separate names    | Combined names  |
59|-------------------|-----------------|
60| `--vec 1 --vec 2` | `--vec 1 2`     |
61
62It is also possible to specify a minimum and maximum number through `->expected(Min,Max)`.  It is also possible to specify a min and max type size for the elements of the container.  It most cases these values will be automatically determined but a user can manually restrict them.
63
64An example of setting up a vector option:
65
66```cpp
67std::vector<int> int_vec;
68app.add_option("--vec", int_vec, "My vector option");
69```
70
71Vectors will be replaced by the parsed content if the option is given on the command line.
72
73A definition of a container for purposes of CLI11 is a type with a `end()`, `insert(...)`, `clear()` and `value_type` definitions.  This includes `vector`, `set`, `deque`, `list`, `forward_iist`, `map`, `unordered_map` and a few others from the standard library, and many other containers from the boost library.
74
75### Containers of containers
76
77Containers of containers are also supported.
78
79```cpp
80std::vector<std::vector<int>> int_vec;
81app.add_option("--vec", int_vec, "My vector of vectors option");
82```
83
84CLI11 inserts a separator sequence at the start of each argument call to separate the vectors.  So unless the separators are injected as part of the command line each call of the option on the command line will result in a separate element of the outer vector.  This can be manually controlled via `inject_separator(true|false)` but in nearly all cases this should be left to the defaults.  To insert of a separator from the command line add a `%%` where the separation should occur.
85
86```bash
87cmd --vec_of_vec 1 2 3 4 %% 1 2
88```
89
90would then result in a container of size 2 with the first element containing 4 values and the second 2.
91
92This separator is also the only way to get values into something like
93
94```cpp
95std::pair<std::vector<int>,std::vector<int>> two_vecs;
96app.add_option("--vec", two_vecs, "pair of vectors");
97```
98
99without calling the argument twice.
100
101Further levels of nesting containers should compile but intermediate layers will only have a single element in the container, so is probably not that useful.
102
103### Nested types
104
105Types can be nested. For example:
106
107```cpp
108std::map<int, std::pair<int,std::string>> map;
109app.add_option("--dict", map, "map of pairs");
110```
111
112will require 3 arguments for each invocation, and multiple sets of 3 arguments can be entered for a single invocation on the command line.
113
114```cpp
115std::map<int, std::pair<int,std::vector<std::string>>> map;
116app.add_option("--dict", map, "map of pairs");
117```
118
119will result in a requirement for 2 integers on each invocation and absorb an unlimited number of strings including 0.
120
121## Option modifiers
122
123When you call `add_option`, you get a pointer to the added option. You can use that to add option modifiers. A full listing of the option modifiers:
124
125| Modifier | Description |
126|----------|-------------|
127| `->required()` | The program will quit if this option is not present. This is `mandatory` in Plumbum, but required options seems to be a more standard term. For compatibility, `->mandatory()` also works. |
128| `->expected(N)` | Take `N` values instead of as many as possible, mainly for vector args. |
129| `->expected(Nmin,Nmax)` | Take between `Nmin` and `Nmax` values. |
130| `->type_size(N)` | specify that each block of values would consist of N elements |
131| `->type_size(Nmin,Nmax)` | specify that each block of values would consist of between Nmin and Nmax elements |
132| `->needs(opt)` | This option requires another option to also be present, opt is an `Option` pointer or a string with the name of the option.  Can be removed with `->remove_needs(opt)` |
133| `->excludes(opt)` | This option cannot be given with `opt` present, opt is an `Option` pointer or a string with the name of the option.  Can be removed with `->remove_excludes(opt)` |
134| `->envname(name)` | Gets the value from the environment if present and not passed on the command line. |
135| `->group(name)` | The help group to put the option in. No effect for positional options. Defaults to `"Options"`. `"Hidden"` will not show up in the help print. |
136| `->description(string)` | Set/change the description |
137| `->ignore_case()` | Ignore the case on the command line (also works on subcommands, does not affect arguments). |
138| `->ignore_underscore()` | Ignore any underscores on the command line (also works on subcommands, does not affect arguments). |
139| `->allow_extra_args()` | Allow extra argument values to be included when an option is passed. Enabled by default for vector options. |
140| `->disable_flag_override()` | specify that flag options cannot be overridden on the command line use `=<newval>` |
141| `->delimiter('<CH>')` | specify a character that can be used to separate elements in a command line argument, default is <none>, common values are ',', and ';' |
142| `->multi_option_policy( CLI::MultiOptionPolicy::Throw)` | Sets the policy for handling multiple arguments if the option was received on the command line several times. `Throw`ing an error is the default, but `TakeLast`, `TakeFirst`, `TakeAll`, and `Join` are also available. See the next four lines for shortcuts to set this more easily. |
143| `->take_last()` | Only use the last option if passed several times. This is always true by default for bool options, regardless of the app default, but can be set to false explicitly with `->multi_option_policy()`. |
144| `->take_first()` | sets `->multi_option_policy(CLI::MultiOptionPolicy::TakeFirst)` |
145| `->take_all()` | sets `->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)` |
146| `->join()` | sets `->multi_option_policy(CLI::MultiOptionPolicy::Join)`, which uses newlines or the specified delimiter to join all arguments into a single string output. |
147| `->join(delim)` | sets `->multi_option_policy(CLI::MultiOptionPolicy::Join)`, which uses `delim` to join all arguments into a single string output. this also sets the delimiter |
148| `->check(Validator)` | perform a check on the returned results to verify they meet some criteria. See [Validators](./validators.md) for more info |
149| `->transform(Validator)` | Run a transforming validator on each value passed. See [Validators](./validators.md) for more info |
150| `->each(void(std::string))` | Run a function on each parsed value, *in order*. |
151| `->default_str(string)` | set a default string for use in the help and as a default value if no arguments are passed and a value is requested |
152| `->default_function(std::string())` | Advanced: Change the function that `capture_default_str()` uses. |
153| `->default_val(value)` | Generate the default string from a value and validate that the value is also valid.  For options that assign directly to a value type the value in that type is also updated.  Value must be convertible to a string(one of known types or have a stream operator). |
154| `->capture_default_str()` | Store the current value attached and display it in the help string. |
155| `->always_capture_default()` | Always run `capture_default_str()` when creating new options. Only useful on an App's `option_defaults`. |
156| `->run_callback_for_default()` | Force the option callback to be executed or the variable set when the `default_val` is used.  |
157| `->force_callback()` | Force the option callback to be executed regardless of whether the option was used or not.  Will use the default_str if available, if no default is given the callback will be executed with an empty string as an argument, which will translate to a default initialized value, which can be compiler dependent |
158|`->trigger_on_parse()` | Have the option callback be triggered when the value is parsed vs. at the end of all parsing, the option callback can potentially be executed multiple times.  Generally only useful if you have a user defined callback or validation check. Or potentially if a vector input is given multiple times as it will clear the results when a repeat option is given via command line.  It will trigger the callbacks once per option call on the command line|
159| `->option_text(string)` | Sets the text between the option name and description. |
160
161The `->check(...)` and `->transform(...)` modifiers can also take a callback function of the form `bool function(std::string)` that runs on every value that the option receives, and returns a value that tells CLI11 whether the check passed or failed.
162
163## Using the `CLI::Option` pointer
164
165Each of the option creation mechanisms returns a pointer to the internally stored option. If you save that pointer, you can continue to access the option, and change setting on it later. The Option object can also be converted to a bool to see if it was passed, or `->count()` can be used to see how many times the option was passed. Since flags are also options, the same methods work on them.
166
167```cpp
168CLI::Option* opt = app.add_flag("--opt");
169
170CLI11_PARSE(app, argv, argc);
171
172if(* opt)
173    std::cout << "Flag received " << opt->count() << " times." << std::endl;
174```
175
176## Inheritance of defaults
177
178One of CLI11's systems to allow customizability without high levels of verbosity is the inheritance system. You can set default values on the parent `App`, and all options and subcommands created from it remember the default values at the point of creation. The default value for Options, specifically, are accessible through the `option_defaults()` method. There are a number of settings that can be set and inherited:
179
180* `group`: The group name starts as "Options"
181* `required`: If the option must be given. Defaults to `false`. Is ignored for flags.
182* `multi_option_policy`: What to do if several copies of an option are passed and one value is expected. Defaults to `CLI::MultiOptionPolicy::Throw`. This is also used for bool flags, but they always are created with the value `CLI::MultiOptionPolicy::TakeLast` regardless of the default, so that multiple bool flags does not cause an error. But you can override that flag by flag.
183* `ignore_case`: Allow any mixture of cases for the option or flag name
184* `ignore_underscore`: Allow any number of underscores in the option or flag name
185* `configurable`:  Specify whether an option can be configured through a config file
186* `disable_flag_override`:  do not allow flag values to be overridden on the command line
187* `always_capture_default`:  specify that the default values should be automatically captured.
188* `delimiter`:  A delimiter to use for capturing multiple values in a single command line string (e.g. --flag="flag,-flag2,flag3")
189
190An example of usage:
191
192```cpp
193app.option_defaults()->ignore_case()->group("Required");
194
195app.add_flag("--CaSeLeSs");
196app.get_group() // is "Required"
197```
198
199Groups are mostly for visual organization, but an empty string for a group name will hide the option.
200
201### Windows style options
202
203You can also set the app setting `app->allow_windows_style_options()` to allow windows style options to also be recognized on the command line:
204
205* `/a` (flag)
206* `/f filename` (option)
207* `/long` (long flag)
208* `/file filename` (space)
209* `/file:filename` (colon)
210* `/long_flag:false` (long flag with : to override the default value)
211
212Windows style options do not allow combining short options or values not separated from the short option like with `-` options. You still specify option names in the same manner as on Linux with single and double dashes when you use the `add_*` functions, and the Linux style on the command line will still work. If a long and a short option share the same name, the option will match on the first one defined.
213
214## Parse configuration
215
216How an option and its arguments are parsed depends on a set of controls that are part of the option structure.  In most circumstances these controls are set automatically based on the type or function used to create the option and the type the arguments are parsed into.  The variables define the size of the underlying type (essentially how many strings make up the type), the expected size (how many groups are expected) and a flag indicating if multiple groups are allowed with a single option.  And these interact with the `multi_option_policy` when it comes time to parse.
217
218### Examples
219
220How options manage this is best illustrated through some examples.
221
222```cpp
223std::string val;
224app.add_option("--opt",val,"description");
225```
226
227creates an option that assigns a value to a `std::string`  When this option is constructed it sets a type_size min and max of 1.  Meaning that the assignment uses a single string.  The Expected size is also set to 1 by default, and `allow_extra_args` is set to false. meaning that each time this option is called 1 argument is expected.  This would also be the case if val were a `double`, `int` or any other single argument types.
228
229now for example
230
231```cpp
232std::pair<int, std::string> val;
233app.add_option("--opt",val,"description");
234```
235
236In this case the typesize is automatically detected to be 2 instead of 1, so the parsing would expect 2 arguments associated with the option.
237
238```cpp
239std::vector<int> val;
240app.add_option("--opt",val,"description");
241```
242
243detects a type size of 1, since the underlying element type is a single string, so the minimum number of strings is 1.  But since it is a vector the expected number can be very big.  The default for a vector is (1<<30), and the allow_extra_args is set to true.  This means that at least 1 argument is expected to follow the option, but arbitrary numbers of arguments may follow.  These are checked if they have the form of an option but if not they are added to the argument.
244
245```cpp
246std::vector<std::tuple<int, double, std::string>> val;
247app.add_option("--opt",val,"description");
248```
249
250gets into the complicated cases where the type size is now 3.  and the expected max is set to a large number and `allow_extra_args` is set to true.  In this case at least 3 arguments are required to follow the option,  and subsequent groups must come in groups of three, otherwise an error will result.
251
252```cpp
253bool val{false};
254app.add_flag("--opt",val,"description");
255```
256
257Using the add_flag methods for creating options creates an option with an expected size of 0, implying no arguments can be passed.
258
259```cpp
260std::complex<double> val;
261app.add_option("--opt",val,"description");
262```
263
264triggers the complex number type which has a min of 1 and max of 2,  so 1 or 2 strings can be passed.  Complex number conversion supports arguments of the form "1+2j" or "1","2", or "1" "2i".  The imaginary number symbols `i` and `j` are interchangeable in this context.
265
266```cpp
267std::vector<std::vector<int>> val;
268app.add_option("--opt",val,"description");
269```
270
271has a type size of 1 to (1<<30).
272
273### Customization
274
275The `type_size(N)`, `type_size(Nmin, Nmax)`, `expected(N)`, `expected(Nmin,Nmax)`, and `allow_extra_args()` can be used to customize an option.  For example
276
277```cpp
278std::string val;
279auto opt=app.add_flag("--opt{vvv}",val,"description");
280opt->expected(0,1);
281```
282
283will create a hybrid option, that can exist on its own in which case the value "vvv" is used or if a value is given that value will be used.
284
285There are some additional options that can be specified to modify an option for specific cases:
286
287* `->run_callback_for_default()` will specify that the callback should be executed when a default_val is set. This is set automatically when appropriate though it can be turned on or off and any user specified callback for an option will be executed when the default value for an option is set.
288
289## Unusual circumstances
290
291There are a few cases where some things break down in the type system managing options and definitions.  Using the `add_option` method defines a lambda function to extract a default value if required.  In most cases this is either straightforward or a failure is detected automatically and handled.  But in a few cases a streaming template is available that several layers down may not actually be defined.  This results in CLI11 not being able to detect this circumstance automatically and will result in compile error.  One specific known case is `boost::optional` if the boost optional_io header is included.  This header defines a template for all boost optional values even if they do not actually have a streaming operator.  For example `boost::optional<std::vector>` does not have a streaming operator but one is detected since it is part of a template.  For these cases a secondary method `app->add_option_no_stream(...)` is provided that bypasses this operation completely and should compile in these cases.
292