Skip to content

Commit

Permalink
style: pre-commit.ci fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Aug 22, 2023
1 parent 0ac4b4e commit cca6e64
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 43 deletions.
13 changes: 9 additions & 4 deletions book/chapters/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ config flag. The second item is the default file name. If that is specified, the
config will try to read that file. The third item is the help string, with a
reasonable default, and the final argument is a boolean (default: false) that
indicates that the configuration file is required and an error will be thrown if
the file is not found and this is set to true. The option pointer returned by `set_config` is the same type as returned by `add_option` and all modifiers including validators, and checks are valid.
the file is not found and this is set to true. The option pointer returned by
`set_config` is the same type as returned by `add_option` and all modifiers
including validators, and checks are valid.

### Adding a default path

Expand Down Expand Up @@ -100,15 +102,18 @@ option name.

### Order of precedence

By default if multiple configuration files are given they are read in reverse order. With the last one given taking precedence over the earlier ones.
This behavior can be changed through the `multi_option_policy`. For example:
By default if multiple configuration files are given they are read in reverse
order. With the last one given taking precedence over the earlier ones. This
behavior can be changed through the `multi_option_policy`. For example:

```cpp
app.set_config("--config")
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll);
```
will read the files in the order given, which may be useful in some circumstances. Using `CLI::MultiOptionPolicy::TakeLast` would work similarly getting the last `N` files given.
will read the files in the order given, which may be useful in some
circumstances. Using `CLI::MultiOptionPolicy::TakeLast` would work similarly
getting the last `N` files given.
## Configure file format
Expand Down
36 changes: 20 additions & 16 deletions book/chapters/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ that to add option modifiers. A full listing of the option modifiers:
| `->allow_extra_args()` | Allow extra argument values to be included when an option is passed. Enabled by default for vector options. |
| `->disable_flag_override()` | specify that flag options cannot be overridden on the command line use `=<newval>` |
| `->delimiter('<CH>')` | specify a character that can be used to separate elements in a command line argument, default is <none>, common values are ',', and ';' |
| `->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`, `Join`, `Reverse`, and `Sum` are also available. See the next four lines for shortcuts to set this more easily. |
| `->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`, `Join`, `Reverse`, and `Sum` are also available. See the next four lines for shortcuts to set this more easily. |
| `->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()`. |
| `->take_first()` | sets `->multi_option_policy(CLI::MultiOptionPolicy::TakeFirst)` |
| `->take_all()` | sets `->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)` |
Expand All @@ -248,21 +248,25 @@ passed or failed.

### Multi Option policy

The Multi option policy can be used to instruct CLI11 what to do when an option is called multiple times and how to return those values in a meaningful way.
There are several options can be set through the `->multi_option_policy( CLI::MultiOptionPolicy::Throw)` option modifier.
`Throw`ing an error is the default, but `TakeLast`, `TakeFirst`, `TakeAll`, `Join`, `Reverse`, and `Sum`

| Value | Description
|-------------|-------------------------------------------------------------------------------------------------------------------------------------------------|
| Throw | Throws an error if more values are given then expected |
| TakeLast | Selects the last expected number of values given |
| TakeFirst | Selects the first expected number of of values given |
| Join | Joins the strings together using the `delimiter` given |
| TakeAll | Takes all the values |
| Sum | If the values are numeric, it sums them and returns the result |
| Reverse | Selects the last expected number of values given and return them in reverse order |

NOTE: For reverse, the index used for an indexed validator is also applied in reverse order index 1 will be the last element and 2 second from last and so on.
The Multi option policy can be used to instruct CLI11 what to do when an option
is called multiple times and how to return those values in a meaningful way.
There are several options can be set through the
`->multi_option_policy( CLI::MultiOptionPolicy::Throw)` option modifier.
`Throw`ing an error is the default, but `TakeLast`, `TakeFirst`, `TakeAll`,
`Join`, `Reverse`, and `Sum`

| Value | Description |
| --------- | --------------------------------------------------------------------------------- |
| Throw | Throws an error if more values are given then expected |
| TakeLast | Selects the last expected number of values given |
| TakeFirst | Selects the first expected number of of values given |
| Join | Joins the strings together using the `delimiter` given |
| TakeAll | Takes all the values |
| Sum | If the values are numeric, it sums them and returns the result |
| Reverse | Selects the last expected number of values given and return them in reverse order |

NOTE: For reverse, the index used for an indexed validator is also applied in
reverse order index 1 will be the last element and 2 second from last and so on.

## Using the `CLI::Option` pointer

Expand Down
1 change: 0 additions & 1 deletion include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,6 @@ class App {
/// Read and process a particular configuration file
void _process_config_file(const std::string &config_file, bool throw_error);


/// Get envname options if not yet passed. Runs on *all* subcommands.
void _process_env();

Expand Down
2 changes: 1 addition & 1 deletion include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum class MultiOptionPolicy : char {
TakeFirst, //!< take only the first Expected number of arguments
Join, //!< merge all the arguments together into a single string via the delimiter character default('\n')
TakeAll, //!< just get all the passed argument regardless
Sum, //!< sum all the arguments together if numerical or concatenate directly without delimiter
Sum, //!< sum all the arguments together if numerical or concatenate directly without delimiter
Reverse, //!< take only the last Expected number of arguments in reverse order
};

Expand Down
4 changes: 2 additions & 2 deletions include/CLI/impl/App_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,8 @@ CLI11_INLINE void App::_process_config_file() {
}
return;
}
for(const auto &config_file:config_files) {
_process_config_file(config_file,config_required||file_given);
for(const auto &config_file : config_files) {
_process_config_file(config_file, config_required || file_given);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions include/CLI/impl/Option_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,8 @@ CLI11_INLINE void Option::_validate_results(results_t &res) const {
if(type_size_max_ > 1) { // in this context index refers to the index in the type
int index = 0;
if(get_items_expected_max() < static_cast<int>(res.size()) &&
(multi_option_policy_ == CLI::MultiOptionPolicy::TakeLast||multi_option_policy_==CLI::MultiOptionPolicy::Reverse)) {
(multi_option_policy_ == CLI::MultiOptionPolicy::TakeLast ||
multi_option_policy_ == CLI::MultiOptionPolicy::Reverse)) {
// create a negative index for the earliest ones
index = get_items_expected_max() - static_cast<int>(res.size());
}
Expand All @@ -518,7 +519,8 @@ CLI11_INLINE void Option::_validate_results(results_t &res) const {
} else {
int index = 0;
if(expected_max_ < static_cast<int>(res.size()) &&
(multi_option_policy_ == CLI::MultiOptionPolicy::TakeLast||multi_option_policy_==CLI::MultiOptionPolicy::Reverse)) {
(multi_option_policy_ == CLI::MultiOptionPolicy::TakeLast ||
multi_option_policy_ == CLI::MultiOptionPolicy::Reverse)) {
// create a negative index for the earliest ones
index = expected_max_ - static_cast<int>(res.size());
}
Expand Down Expand Up @@ -554,10 +556,10 @@ CLI11_INLINE void Option::_reduce_results(results_t &out, const results_t &origi
// Allow multi-option sizes (including 0)
std::size_t trim_size = std::min<std::size_t>(
static_cast<std::size_t>(std::max<int>(get_items_expected_max(), 1)), original.size());
if(original.size() != trim_size || trim_size>1) {
if(original.size() != trim_size || trim_size > 1) {
out.assign(original.end() - static_cast<results_t::difference_type>(trim_size), original.end());
}
std::reverse(out.begin(),out.end());
std::reverse(out.begin(), out.end());
} break;
case MultiOptionPolicy::TakeFirst: {
std::size_t trim_size = std::min<std::size_t>(
Expand Down
28 changes: 13 additions & 15 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,21 @@ TEST_CASE_METHOD(TApp, "OneFlagShortValuesAs", "[app]") {
auto vec = opt->as<std::vector<int>>();
CHECK(1 == vec[0]);
CHECK(2 == vec[1]);

flg->multi_option_policy(CLI::MultiOptionPolicy::Sum);
vec = opt->as<std::vector<int>>();
CHECK(3 == vec[0]);
CHECK(vec.size()==1);
CHECK(vec.size() == 1);

flg->multi_option_policy(CLI::MultiOptionPolicy::Join);
CHECK("1\n2" == opt->as<std::string>());
flg->delimiter(',');
CHECK("1,2" == opt->as<std::string>());
flg->multi_option_policy(CLI::MultiOptionPolicy::Reverse)->expected(1,300);
flg->multi_option_policy(CLI::MultiOptionPolicy::Reverse)->expected(1, 300);
vec = opt->as<std::vector<int>>();
REQUIRE(vec.size()==2U);
REQUIRE(vec.size() == 2U);
CHECK(2 == vec[0]);
CHECK(1 == vec[1]);

}

TEST_CASE_METHOD(TApp, "OneFlagShortWindows", "[app]") {
Expand Down Expand Up @@ -878,28 +877,27 @@ TEST_CASE_METHOD(TApp, "SumOptString", "[app]") {
CHECK("i2" == val);
}


TEST_CASE_METHOD(TApp, "ReverseOpt", "[app]") {

std::vector<std::string> val;
auto *opt1=app.add_option("--val", val)->multi_option_policy(CLI::MultiOptionPolicy::Reverse);
auto *opt1 = app.add_option("--val", val)->multi_option_policy(CLI::MultiOptionPolicy::Reverse);

args = {"--val=string1", "--val=string2", "--val", "string3", "string4"};

run();

CHECK(val.size()==4U);
CHECK(val.size() == 4U);

CHECK(val.front()=="string4");
CHECK(val.back()=="string1");
CHECK(val.front() == "string4");
CHECK(val.back() == "string1");

opt1->expected(1,2);
opt1->expected(1, 2);
run();
CHECK(val.size()==2U);
CHECK(val.size() == 2U);

CHECK(val.front()=="string4");
CHECK(val.back()=="string3");
CHECK(opt1->get_multi_option_policy()==CLI::MultiOptionPolicy::Reverse);
CHECK(val.front() == "string4");
CHECK(val.back() == "string3");
CHECK(opt1->get_multi_option_policy() == CLI::MultiOptionPolicy::Reverse);
}

TEST_CASE_METHOD(TApp, "JoinOpt2", "[app]") {
Expand Down

0 comments on commit cca6e64

Please sign in to comment.