From 4b43a7d3f055a18958b14062e591fdd62bd8a449 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Sat, 31 Jul 2021 09:00:24 -0700 Subject: [PATCH 01/11] Add some missing modifiers on the options to the docs and clarify some of them. --- README.md | 11 +++++++---- book/chapters/options.md | 10 +++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3fb84f7e8..24c7a8592 100644 --- a/README.md +++ b/README.md @@ -336,10 +336,12 @@ Before parsing, you can set the following options: * `->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. * `->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. +* `->expected(MIN,MAX)`: Set a range of expected values to accompany an option. `expected(0,1)` is the equivalent of making a flag. * `->type_name(typename)`: Set the name of an Option's type (`type_name_fn` allows a function instead) -* `->type_size(N)`: Set the intrinsic size of an option. The parser will require multiples of this number if negative. -* `->needs(opt)`: This option requires another option to also be present, opt is an `Option` pointer. -* `->excludes(opt)`: This option cannot be given with `opt` present, opt is an `Option` pointer. +* `->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. +* `->type_size(MIN,MAX)`: Set the intrinsic size of an option to a range. +* `->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 +* `->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)` * `->envname(name)`: Gets the value from the environment if present and not passed on the command line. * `->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). * `->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: * `->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. * `->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. * `->description(str)`: Set/change the description. -* `->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). +* `->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()` * `->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 * `->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. * `->transform(std::string(std::string &), validator_name="",validator_description=")`: Converts the input string into the output string, in-place in the parsed options. @@ -360,6 +362,7 @@ Before parsing, you can set the following options: * `->always_capture_default()`: Always run `capture_default_str()` when creating new options. Only useful on an App's `option_defaults`. * `->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. * `->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). +* `->run_callback_for_default()`: This will force the option callback to be executed or the variable set regardless of whether the option was passed or not. Can be used to force an output variable to be set to the default_str. * `->option_text(string)`: Sets the text between the option name and description. 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` of results. diff --git a/book/chapters/options.md b/book/chapters/options.md index 60a1a4679..424cc6470 100644 --- a/book/chapters/options.md +++ b/book/chapters/options.md @@ -129,8 +129,8 @@ When you call `add_option`, you get a pointer to the added option. You can use t | `->expected(Nmin,Nmax)` | Take between `Nmin` and `Nmax` values. | | `->type_size(N)` | specify that each block of values would consist of N elements | | `->type_size(Nmin,Nmax)` | specify that each block of values would consist of between Nmin and Nmax elements | -| `->needs(opt)` | This option requires another option to also be present, opt is an `Option` pointer. | -| `->excludes(opt)` | This option cannot be given with `opt` present, opt is an `Option` pointer. | +| `->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)` | +| `->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)` | | `->envname(name)` | Gets the value from the environment if present and not passed on the command line. | | `->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. | | `->description(string)` | Set/change the description | @@ -149,8 +149,12 @@ When you call `add_option`, you get a pointer to the added option. You can use t | `->transform(Validator)` | Run a transforming validator on each value passed. See [Validators](./validators.md) for more info | | `->each(void(std::string))` | Run a function on each parsed value, *in order*. | | `->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 | -| `->default_function(string())` | Advanced: Change the function that `capture_default_str()` uses. | +| `->default_function(std::string())` | Advanced: Change the function that `capture_default_str()` uses. | | `->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). | +| `->capture_default_str()` | Store the current value attached and display it in the help string. | +| `->always_capture_default()` | Always run `capture_default_str()` when creating new options. Only useful on an App's `option_defaults`. | +| `->run_callback_for_default()` | Force the option callback to be executed or the variable set regardless of whether the option was passed or not. Can be used to force an output variable to be set to the default_str. | +| `->option_text(string)` | Sets the text between the option name and description. | 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. From cd92b5a8067285a359cd239b933a8f81f06e4887 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Aug 2021 15:06:10 +0000 Subject: [PATCH 02/11] style: pre-commit.ci fixes --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 24c7a8592..9ed940085 100644 --- a/README.md +++ b/README.md @@ -336,9 +336,9 @@ Before parsing, you can set the following options: * `->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. * `->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. -* `->expected(MIN,MAX)`: Set a range of expected values to accompany an option. `expected(0,1)` is the equivalent of making a flag. +* `->expected(MIN,MAX)`: Set a range of expected values to accompany an option. `expected(0,1)` is the equivalent of making a flag. * `->type_name(typename)`: Set the name of an Option's type (`type_name_fn` allows a function instead) -* `->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. +* `->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. * `->type_size(MIN,MAX)`: Set the intrinsic size of an option to a range. * `->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 * `->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)` @@ -362,7 +362,7 @@ Before parsing, you can set the following options: * `->always_capture_default()`: Always run `capture_default_str()` when creating new options. Only useful on an App's `option_defaults`. * `->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. * `->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). -* `->run_callback_for_default()`: This will force the option callback to be executed or the variable set regardless of whether the option was passed or not. Can be used to force an output variable to be set to the default_str. +* `->run_callback_for_default()`: This will force the option callback to be executed or the variable set regardless of whether the option was passed or not. Can be used to force an output variable to be set to the default_str. * `->option_text(string)`: Sets the text between the option name and description. 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` of results. From 1c27ee8fdfeebbe9ef8495aa9fc09409ed609e96 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 2 Aug 2021 07:20:53 -0700 Subject: [PATCH 03/11] add a more clear force callback and callback on parse modifier for options. --- include/CLI/Option.hpp | 37 ++++++++++++++++++++++++++++++++----- tests/AppTest.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/include/CLI/Option.hpp b/include/CLI/Option.hpp index 11ae2c05a..cdbda3743 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -337,6 +337,10 @@ class Option : public OptionBase