diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c8c41c4..cf1fb251 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ## Version 1.2 (in progress) + +* `set_default_str` now only sets string, and `set_default_val` will evaluate the default string given [#26](https://github.com/CLIUtils/CLI11/issues/26) * Required positionals now take priority over subcommands [#23](https://github.com/CLIUtils/CLI11/issues/23) +* Extra requirements enforced by Travis ## Version 1.1 diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index cedef560..cdb91abc 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -261,7 +261,7 @@ class App { if(defaulted) { std::stringstream out; out << variable; - opt->set_default_val(out.str()); + opt->set_default_str(out.str()); } return opt; } @@ -307,7 +307,7 @@ class App { Option *opt = add_option(name, fun, description, defaulted); opt->set_custom_option(detail::type_name(), -1, true); if(defaulted) - opt->set_default_val("[" + detail::join(variable) + "]"); + opt->set_default_str("[" + detail::join(variable) + "]"); return opt; } @@ -410,7 +410,7 @@ class App { if(defaulted) { std::stringstream out; out << member; - opt->set_default_val(out.str()); + opt->set_default_str(out.str()); } return opt; } @@ -473,7 +473,7 @@ class App { typeval += " in {" + detail::join(options) + "}"; opt->set_custom_option(typeval); if(defaulted) { - opt->set_default_val(member); + opt->set_default_str(member); } return opt; } @@ -500,7 +500,7 @@ class App { if(defaulted) { std::stringstream out; out << variable; - opt->set_default_val(out.str()); + opt->set_default_str(out.str()); } return opt; } diff --git a/include/CLI/Option.hpp b/include/CLI/Option.hpp index 7dbe40d1..2c01a665 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -431,7 +431,16 @@ class Option { } /// Set the default value string representation - void set_default_val(std::string val) { defaultval_ = val; } + void set_default_str(std::string val) { defaultval_ = val; } + + /// Set the default value string representation and evaluate + void set_default_val(std::string val) { + set_default_str(val); + auto old_results = results_; + results_ = {val}; + run_callback(); + results_ = std::move(old_results); + } /// Set the type name displayed on this option void set_type_name(std::string val) { typeval_ = val; } diff --git a/tests/HelpTest.cpp b/tests/HelpTest.cpp index 863f4225..a1ad292d 100644 --- a/tests/HelpTest.cpp +++ b/tests/HelpTest.cpp @@ -194,16 +194,22 @@ TEST(THelp, ManualSetters) { CLI::App app{"My prog"}; - int x; + int x = 1; CLI::Option *op1 = app.add_option("--op", x); - op1->set_default_val("12"); + op1->set_default_str("12"); op1->set_type_name("BIGGLES"); + EXPECT_EQ(x, 1); std::string help = app.help(); EXPECT_THAT(help, HasSubstr("=12")); EXPECT_THAT(help, HasSubstr("BIGGLES")); + + op1->set_default_val("14"); + EXPECT_EQ(x, 14); + help = app.help(); + EXPECT_THAT(help, HasSubstr("=14")); } TEST(THelp, Subcom) { diff --git a/tests/NewParseTest.cpp b/tests/NewParseTest.cpp index 07042a5d..01c656d4 100644 --- a/tests/NewParseTest.cpp +++ b/tests/NewParseTest.cpp @@ -23,7 +23,7 @@ add_option(CLI::App &app, std::string name, cx &variable, std::string descriptio if(defaulted) { std::stringstream out; out << variable; - opt->set_default_val(out.str()); + opt->set_default_str(out.str()); } return opt; }