1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Adding each()

This commit is contained in:
Henry Fredrick Schreiner 2018-05-09 16:42:40 +02:00 committed by Henry Schreiner
parent e8b45de6ec
commit 47d5ed1453
4 changed files with 29 additions and 0 deletions

View File

@ -36,6 +36,7 @@ Validators are now much more powerful [#118], all built in validators upgraded t
Other changes:
* Added `->each()` to make adding custom callbacks easier [#126]
* Added filter argument to `get_subcommands`, `get_options`; use empty filter `{}` to avoid filtering
* Added `get_groups()` to get groups
* Added getters for the missing parts of options (help no longer uses any private parts)
@ -60,6 +61,7 @@ Other changes:
[#119]: https://github.com/CLIUtils/CLI11/pull/119
[#120]: https://github.com/CLIUtils/CLI11/pull/120
[#121]: https://github.com/CLIUtils/CLI11/pull/121
[#126]: https://github.com/CLIUtils/CLI11/pull/126
### Version 1.5.3: Compiler compatibility
This version fixes older AppleClang compilers by removing the optimization for casting. The minimum version of Boost Optional supported has been clarified to be 1.58. CUDA 7.0 NVCC is now supported.

View File

@ -192,6 +192,7 @@ The add commands return a pointer to an internally stored `Option`. If you set t
* `->check(CLI::NonexistentPath)`: Requires that the path does not exist.
* `->check(CLI::Range(min,max))`: Requires that the option be between min and max (make sure to use floating point if needed). Min defaults to 0.
* `->transform(std::string(std::string))`: Converts the input string into the output string, in-place in the parsed options.
* `->each(void(std::string)>`: Run this function on each value received, as it is received.
* `->configurable(false)`: Disable this option from being in a configuration file.
These options return the `Option` pointer, so you can chain them together, and even skip storing the pointer entirely. Check 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 `check` and `transform` use the same underlying mechanism, you can chain as many as you want, and they will be executed in order. If you just want to see the unconverted values, use `.results()` to get the `std::vector<std::string>` of results. Validate can also be a subclass of `CLI::Validator`, in which case it can also set the type name and can be combined with `&` and `|` (all built-in validators are this sort).

View File

@ -308,6 +308,15 @@ class Option : public OptionBase<Option> {
return this;
}
/// Adds a user supplied function to run on each item passed in (communicate though lambda capture)
Option *each(std::function<void(std::string)> func) {
validators_.emplace_back([func](std::string &inout) {
func(inout);
return std::string();
});
return this;
}
/// Sets required options
Option *needs(Option *opt) {
auto tup = requires_.insert(opt);

View File

@ -1478,6 +1478,23 @@ TEST_F(TApp, ThrowingTransform) {
}
}
// This was added to make running a simple function on each item easier
TEST_F(TApp, EachItem) {
std::vector<std::string> results;
std::vector<std::string> dummy;
auto opt = app.add_option("--vec", dummy);
opt->each([&results](std::string item) { results.push_back(item); });
args = {"--vec", "one", "two", "three"};
run();
EXPECT_EQ(results, dummy);
}
// #87
TEST_F(TApp, CustomDoubleOption) {