diff --git a/README.md b/README.md index b1a353ea..2f09b1e6 100644 --- a/README.md +++ b/README.md @@ -331,6 +331,8 @@ Before parsing, you can set the following options: `->capture_default_str()`: 🆕 Store the current value attached and display it in the help string. - `->default_function(std::string())`: 🆕 Advanced: Change the function that `capture_default_str()` uses. - `->always_capture_default()`: 🆕 Always run `capture_default_str()` when creating new options. Only useful on an App's `option_defaults`. +- `default_str(string)`: Set the default string directly. This string will also be used as a default value if no arguments are passed and the value is requested. +- `default_val(value)`: 🚧 Generate the default string from a value and validate that the value is also valid. For options that assign directly to a value type the value in that type is also updated. Value must be convertible to a string(one of known types or a stream operator). These options return the `Option` pointer, so you can chain them together, and even skip storing the pointer entirely. The `each` function 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 `each`, `check` and `transform` use the same underlying mechanism, you can chain as many as you want, and they will be executed in order. Operations added through `transform` are executed first in reverse order of addition, and `check` and `each` are run following the transform functions in order of addition. If you just want to see the unconverted values, use `.results()` to get the `std::vector` of results. @@ -990,4 +992,3 @@ CLI11 was developed at the [University of Cincinnati][] to support of the [GooFi [hunter]: https://docs.hunter.sh/en/latest/packages/pkg/CLI11.html [standard readme style]: https://github.com/RichardLitt/standard-readme [argparse]: https://github.com/p-ranav/argparse - diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index fe93c39d..204283fc 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -601,12 +601,13 @@ class App { return CLI::detail::checked_to_string(variable); }); opt->type_name(detail::type_name()); - // these must be actual variables since (std::max) sometimes is defined in terms of references and references + // these must be actual lvalues since (std::max) sometimes is defined in terms of references and references // to structs used in the evaluation can be temporary so that would cause issues. auto Tcount = detail::type_count::value; auto XCcount = detail::type_count::value; opt->type_size((std::max)(Tcount, XCcount)); opt->expected(detail::expected_count::value); + opt->run_callback_for_default(); return opt; } @@ -754,7 +755,7 @@ class App { CLI::callback_t fun = [&flag_result](const CLI::results_t &res) { return CLI::detail::lexical_cast(res[0], flag_result); }; - return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description)); + return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description))->run_callback_for_default(); } /// Vector version to capture multiple flags. @@ -772,7 +773,8 @@ class App { return retval; }; return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description)) - ->multi_option_policy(MultiOptionPolicy::TakeAll); + ->multi_option_policy(MultiOptionPolicy::TakeAll) + ->run_callback_for_default(); } /// Add option for callback that is triggered with a true flag and takes no arguments @@ -911,7 +913,7 @@ class App { CLI::Option *opt = add_option(option_name, std::move(fun), std::move(option_description), defaulted, default_function); - opt->type_name(label)->type_size(1, 2)->delimiter('+'); + opt->type_name(label)->type_size(1, 2)->delimiter('+')->run_callback_for_default(); return opt; } diff --git a/include/CLI/Option.hpp b/include/CLI/Option.hpp index eee91f53..bcc88e01 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -324,6 +324,8 @@ class Option : public OptionBase