Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bits of cleanup #126

Merged
merged 2 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
10 changes: 2 additions & 8 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,7 @@ class App {
T &variable, ///< The variable to set
std::string description = "") {

std::string simple_name = CLI::detail::split(name, ',').at(0);
CLI::callback_t fun = [&variable, simple_name](CLI::results_t res) {
return detail::lexical_cast(res[0], variable);
};
CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };

Option *opt = add_option(name, fun, description, false);
opt->set_custom_option(detail::type_name<T>());
Expand All @@ -345,10 +342,7 @@ class App {
std::string description,
bool defaulted) {

std::string simple_name = CLI::detail::split(name, ',').at(0);
CLI::callback_t fun = [&variable, simple_name](CLI::results_t res) {
return detail::lexical_cast(res[0], variable);
};
CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };

Option *opt = add_option(name, fun, description, defaulted);
opt->set_custom_option(detail::type_name<T>());
Expand Down
9 changes: 9 additions & 0 deletions include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down