1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

set_default_str and set_default_val update (#27)

* set_default_str and set_default_val update

* Fixes for style checking
This commit is contained in:
Henry Schreiner 2017-09-01 23:23:31 -04:00 committed by GitHub
parent 54114d0948
commit 93311928d7
5 changed files with 27 additions and 9 deletions

View File

@ -1,6 +1,9 @@
## Version 1.2 (in progress) ## 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) * Required positionals now take priority over subcommands [#23](https://github.com/CLIUtils/CLI11/issues/23)
* Extra requirements enforced by Travis
## Version 1.1 ## Version 1.1

View File

@ -261,7 +261,7 @@ class App {
if(defaulted) { if(defaulted) {
std::stringstream out; std::stringstream out;
out << variable; out << variable;
opt->set_default_val(out.str()); opt->set_default_str(out.str());
} }
return opt; return opt;
} }
@ -307,7 +307,7 @@ class App {
Option *opt = add_option(name, fun, description, defaulted); Option *opt = add_option(name, fun, description, defaulted);
opt->set_custom_option(detail::type_name<T>(), -1, true); opt->set_custom_option(detail::type_name<T>(), -1, true);
if(defaulted) if(defaulted)
opt->set_default_val("[" + detail::join(variable) + "]"); opt->set_default_str("[" + detail::join(variable) + "]");
return opt; return opt;
} }
@ -410,7 +410,7 @@ class App {
if(defaulted) { if(defaulted) {
std::stringstream out; std::stringstream out;
out << member; out << member;
opt->set_default_val(out.str()); opt->set_default_str(out.str());
} }
return opt; return opt;
} }
@ -473,7 +473,7 @@ class App {
typeval += " in {" + detail::join(options) + "}"; typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval); opt->set_custom_option(typeval);
if(defaulted) { if(defaulted) {
opt->set_default_val(member); opt->set_default_str(member);
} }
return opt; return opt;
} }
@ -500,7 +500,7 @@ class App {
if(defaulted) { if(defaulted) {
std::stringstream out; std::stringstream out;
out << variable; out << variable;
opt->set_default_val(out.str()); opt->set_default_str(out.str());
} }
return opt; return opt;
} }

View File

@ -431,7 +431,16 @@ class Option {
} }
/// Set the default value string representation /// 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 /// Set the type name displayed on this option
void set_type_name(std::string val) { typeval_ = val; } void set_type_name(std::string val) { typeval_ = val; }

View File

@ -194,16 +194,22 @@ TEST(THelp, ManualSetters) {
CLI::App app{"My prog"}; CLI::App app{"My prog"};
int x; int x = 1;
CLI::Option *op1 = app.add_option("--op", x); CLI::Option *op1 = app.add_option("--op", x);
op1->set_default_val("12"); op1->set_default_str("12");
op1->set_type_name("BIGGLES"); op1->set_type_name("BIGGLES");
EXPECT_EQ(x, 1);
std::string help = app.help(); std::string help = app.help();
EXPECT_THAT(help, HasSubstr("=12")); EXPECT_THAT(help, HasSubstr("=12"));
EXPECT_THAT(help, HasSubstr("BIGGLES")); 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) { TEST(THelp, Subcom) {

View File

@ -23,7 +23,7 @@ add_option(CLI::App &app, std::string name, cx &variable, std::string descriptio
if(defaulted) { if(defaulted) {
std::stringstream out; std::stringstream out;
out << variable; out << variable;
opt->set_default_val(out.str()); opt->set_default_str(out.str());
} }
return opt; return opt;
} }