From 5c68508c76a5b8eb6ae652a61f9441a9f44766d8 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Wed, 9 May 2018 16:09:19 +0200 Subject: [PATCH 1/2] Dropping a few lines --- include/CLI/App.hpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index c26e23965..c3fd81db1 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -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()); @@ -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()); From 9eb75114160389d47916dae26db47e11b8d521a1 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Wed, 9 May 2018 16:42:40 +0200 Subject: [PATCH 2/2] Adding each() --- CHANGELOG.md | 2 ++ README.md | 1 + include/CLI/Option.hpp | 9 +++++++++ tests/AppTest.cpp | 17 +++++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ceb394a1..2ec3c0342 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 315ecc8d4..589f8e5ef 100644 --- a/README.md +++ b/README.md @@ -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` 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). diff --git a/include/CLI/Option.hpp b/include/CLI/Option.hpp index 93991ba82..db625576e 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -308,6 +308,15 @@ class Option : public OptionBase