You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add some missing modifiers on the options to the docs and clarify some of them.
* style: pre-commit.ci fixes
* add a more clear force callback and callback on parse modifier for options.
* update the book with new modifiers
* update documentation and add tests
* style: pre-commit.ci fixes
* more updates to the readme
* update formatting
* rework the trigger_on_parse to better support more complex option types
* fix formatting errors
* Update include/CLI/Option.hpp
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <[email protected]>
Co-authored-by: Henry Schreiner <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+14-9Lines changed: 14 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -336,10 +336,12 @@ Before parsing, you can set the following options:
336
336
337
337
*`->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.
338
338
*`->expected(N)`: Take `N` values instead of as many as possible, only for vector args. If negative, require at least `-N`; end with `--` or another recognized option or subcommand.
339
+
*`->expected(MIN,MAX)`: Set a range of expected values to accompany an option. `expected(0,1)` is the equivalent of making a flag.
339
340
*`->type_name(typename)`: Set the name of an Option's type (`type_name_fn` allows a function instead)
340
-
*`->type_size(N)`: Set the intrinsic size of an option. The parser will require multiples of this number if negative.
341
-
*`->needs(opt)`: This option requires another option to also be present, opt is an `Option` pointer.
342
-
*`->excludes(opt)`: This option cannot be given with `opt` present, opt is an `Option` pointer.
341
+
*`->type_size(N)`: Set the intrinsic size of an option value. The parser will require multiples of this number if negative. Most of the time this is detected automatically though can be modified for specific use cases.
342
+
*`->type_size(MIN,MAX)`: Set the intrinsic size of an option to a range.
343
+
*`->needs(opt)`: This option requires another option to also be present, opt is an `Option` pointer. Options can be removed from the `needs` with `remove_needs(opt)`. The option can also be specified with a string containing the name of the option
344
+
*`->excludes(opt)`: This option cannot be given with `opt` present, opt is an `Option` pointer. Can also be given as a string containing the name of the option. Options can be removed from the excludes list with `->remove_excludes(opt)`
343
345
*`->envname(name)`: Gets the value from the environment if present and not passed on the command line.
344
346
*`->group(name)`: The help group to put the option in. No effect for positional options. Defaults to `"Options"`. `""` will not show up in the help print (hidden).
345
347
*`->ignore_case()`: Ignore the case on the command line (also works on subcommands, does not affect arguments).
@@ -348,7 +350,7 @@ Before parsing, you can set the following options:
348
350
*`->allow_extra_args(true/false)`: 🆕 If set to true the option will take an unlimited number of arguments like a vector, if false it will limit the number of arguments to the size of the type used in the option. Default value depends on the nature of the type use, containers default to true, others default to false.
349
351
*`->delimiter(char)`: Allows specification of a custom delimiter for separating single arguments into vector arguments, for example specifying `->delimiter(',')` on an option would result in `--opt=1,2,3` producing 3 elements of a vector and the equivalent of --opt 1 2 3 assuming opt is a vector value.
350
352
*`->description(str)`: Set/change the description.
351
-
*`->multi_option_policy(CLI::MultiOptionPolicy::Throw)`: Set the multi-option policy. Shortcuts available: `->take_last()`, `->take_first()`,`->take_all()`, and `->join()`. This will only affect options expecting 1 argument or bool flags (which do not inherit their default but always start with a specific policy).
353
+
*`->multi_option_policy(CLI::MultiOptionPolicy::Throw)`: Set the multi-option policy. Shortcuts available: `->take_last()`, `->take_first()`,`->take_all()`, and `->join()`. This will only affect options expecting 1 argument or bool flags (which do not inherit their default but always start with a specific policy).`->join(delim)` can also be used to join with a specific delimiter. This equivalent to calling `->delimiter(delim)` and `->join()`
352
354
*`->check(std::string(const std::string &), validator_name="",validator_description="")`: Define a check function. The function should return a non empty string with the error message if the check fails
353
355
*`->check(Validator)`: Use a Validator object to do the check see [Validators](#validators) for a description of available Validators and how to create new ones.
354
356
*`->transform(std::string(std::string &), validator_name="",validator_description=")`: Converts the input string into the output string, in-place in the parsed options.
@@ -358,9 +360,12 @@ Before parsing, you can set the following options:
358
360
*`->capture_default_str()`: Store the current value attached and display it in the help string.
359
361
*`->default_function(std::string())`: Advanced: Change the function that `capture_default_str()` uses.
360
362
*`->always_capture_default()`: Always run `capture_default_str()` when creating new options. Only useful on an App's `option_defaults`.
361
-
*`->default_str(string)`: Set the default string directly. This string will also be used as a default value if no arguments are passed and the value is requested.
362
-
*`->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).
363
+
*`->default_str(string)`: Set the default string directly (NO VALIDATION OR CALLBACKS). This string will also be used as a default value if no arguments are passed and the value is requested.
364
+
*`->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). The callback may be triggered if the `run_callback_for_default` is set.
365
+
*`->run_callback_for_default()`: This will force the option callback to be executed or the variable set when the default_val is set.
363
366
*`->option_text(string)`: Sets the text between the option name and description.
367
+
*`->force_callback()`: Causes the option callback or value set to be triggered even if the option was not present in parsing.
368
+
*`->trigger_on_parse()`: if set, causes the callback and all associated validation checks for the option to be executed when the option value is parsed vs. at the end of all parsing. This could cause the callback to be executed multiple times.
364
369
365
370
These options return the `Option` pointer, so you can chain them together, and even skip storing the pointer entirely. The `each` function takes any function that has the signature `void(const std::string&)`; it should throw a `ValidationError` when validation fails. The help message will have the name of the parent option prepended. Since `each`, `check` and `transform` use the same underlying mechanism, you can chain as many as you want, and they will be executed in order. Operations added through `transform` are executed first in reverse order of addition, and `check` and `each` are run following the transform functions in order of addition. If you just want to see the unconverted values, use `.results()` to get the `std::vector<std::string>` of results.
366
371
@@ -536,9 +541,9 @@ Validators have a few functions to query the current values:
536
541
537
542
In most cases, the fastest and easiest way is to return the results through a callback or variable specified in one of the `add_*` functions. But there are situations where this is not possible or desired. For these cases the results may be obtained through one of the following functions. Please note that these functions will do any type conversions and processing during the call so should not used in performance critical code:
538
543
539
-
*`results()`: Retrieves a vector of strings with all the results in the order they were given.
540
-
*`results(variable_to_bind_to)`: Gets the results according to the MultiOptionPolicy and converts them just like the `add_option_function` with a variable.
541
-
*`Value=as<type>()`: Returns the result or default value directly as the specified type if possible, can be vector to return all results, and a non-vector to get the result according to the MultiOptionPolicy in place.
544
+
*`->results()`: Retrieves a vector of strings with all the results in the order they were given.
545
+
*`->results(variable_to_bind_to)`: Gets the results according to the MultiOptionPolicy and converts them just like the `add_option_function` with a variable.
546
+
*`Value=opt->as<type>()`: Returns the result or default value directly as the specified type if possible, can be vector to return all results, and a non-vector to get the result according to the MultiOptionPolicy in place.
Copy file name to clipboardExpand all lines: book/chapters/options.md
+10-4Lines changed: 10 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ You can use any C++ int-like type, not just `int`. CLI11 understands the followi
20
20
21
21
| Type | CLI11 |
22
22
|-------------|-------|
23
-
| number like | Integers, floats, bools, or any type that can be constructed from an integer or floating point number |
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
24
| string-like | std\::string, or anything that can be constructed from or assigned a std\::string |
25
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
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" |
@@ -129,8 +129,8 @@ When you call `add_option`, you get a pointer to the added option. You can use t
129
129
|`->expected(Nmin,Nmax)`| Take between `Nmin` and `Nmax` values. |
130
130
|`->type_size(N)`| specify that each block of values would consist of N elements |
131
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.|
133
-
|`->excludes(opt)`| This option cannot be given with `opt` present, opt is an `Option` pointer.|
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
134
|`->envname(name)`| Gets the value from the environment if present and not passed on the command line. |
135
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
136
|`->description(string)`| Set/change the description |
@@ -149,8 +149,14 @@ When you call `add_option`, you get a pointer to the added option. You can use t
149
149
|`->transform(Validator)`| Run a transforming validator on each value passed. See [Validators](./validators.md) for more info |
150
150
|`->each(void(std::string))`| Run a function on each parsed value, *in order*. |
151
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(string())`| Advanced: Change the function that `capture_default_str()` uses. |
152
+
|`->default_function(std::string())`| Advanced: Change the function that `capture_default_str()` uses. |
153
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. |
154
160
155
161
The `->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.
0 commit comments