Commit 47d5ed14531031f93f96fe8c15864d1dde3de181

Authored by Henry Fredrick Schreiner
Committed by Henry Schreiner
1 parent e8b45de6

Adding each()

CHANGELOG.md
... ... @@ -36,6 +36,7 @@ Validators are now much more powerful [#118], all built in validators upgraded t
36 36  
37 37 Other changes:
38 38  
  39 +* Added `->each()` to make adding custom callbacks easier [#126]
39 40 * Added filter argument to `get_subcommands`, `get_options`; use empty filter `{}` to avoid filtering
40 41 * Added `get_groups()` to get groups
41 42 * Added getters for the missing parts of options (help no longer uses any private parts)
... ... @@ -60,6 +61,7 @@ Other changes:
60 61 [#119]: https://github.com/CLIUtils/CLI11/pull/119
61 62 [#120]: https://github.com/CLIUtils/CLI11/pull/120
62 63 [#121]: https://github.com/CLIUtils/CLI11/pull/121
  64 +[#126]: https://github.com/CLIUtils/CLI11/pull/126
63 65  
64 66 ### Version 1.5.3: Compiler compatibility
65 67 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.
... ...
README.md
... ... @@ -192,6 +192,7 @@ The add commands return a pointer to an internally stored `Option`. If you set t
192 192 * `->check(CLI::NonexistentPath)`: Requires that the path does not exist.
193 193 * `->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.
194 194 * `->transform(std::string(std::string))`: Converts the input string into the output string, in-place in the parsed options.
  195 +* `->each(void(std::string)>`: Run this function on each value received, as it is received.
195 196 * `->configurable(false)`: Disable this option from being in a configuration file.
196 197  
197 198 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).
... ...
include/CLI/Option.hpp
... ... @@ -308,6 +308,15 @@ class Option : public OptionBase&lt;Option&gt; {
308 308 return this;
309 309 }
310 310  
  311 + /// Adds a user supplied function to run on each item passed in (communicate though lambda capture)
  312 + Option *each(std::function<void(std::string)> func) {
  313 + validators_.emplace_back([func](std::string &inout) {
  314 + func(inout);
  315 + return std::string();
  316 + });
  317 + return this;
  318 + }
  319 +
311 320 /// Sets required options
312 321 Option *needs(Option *opt) {
313 322 auto tup = requires_.insert(opt);
... ...
tests/AppTest.cpp
... ... @@ -1478,6 +1478,23 @@ TEST_F(TApp, ThrowingTransform) {
1478 1478 }
1479 1479 }
1480 1480  
  1481 +// This was added to make running a simple function on each item easier
  1482 +TEST_F(TApp, EachItem) {
  1483 +
  1484 + std::vector<std::string> results;
  1485 + std::vector<std::string> dummy;
  1486 +
  1487 + auto opt = app.add_option("--vec", dummy);
  1488 +
  1489 + opt->each([&results](std::string item) { results.push_back(item); });
  1490 +
  1491 + args = {"--vec", "one", "two", "three"};
  1492 +
  1493 + run();
  1494 +
  1495 + EXPECT_EQ(results, dummy);
  1496 +}
  1497 +
1481 1498 // #87
1482 1499 TEST_F(TApp, CustomDoubleOption) {
1483 1500  
... ...