From 47d5ed14531031f93f96fe8c15864d1dde3de181 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Wed, 9 May 2018 16:42:40 +0200 Subject: [PATCH] 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 0ceb394a..2ec3c034 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 315ecc8d..589f8e5e 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 93991ba8..db625576 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -308,6 +308,15 @@ class Option : public OptionBase