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

Rename to set_* on options and add return this

Now use type_name and type_size instead of set_custom_option.
This commit is contained in:
Henry Schreiner 2018-06-16 00:36:55 +02:00 committed by Henry Schreiner
parent 23cedc12e8
commit bf2bc39c30
8 changed files with 63 additions and 51 deletions

View File

@ -36,6 +36,7 @@ Validators are now much more powerful [#118], all built in validators upgraded t
Other changes:
* Dropped `set_*` names on options, using `type_name` and `type_size` instead of `set_custom_option`. Methods return this.
* 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

View File

@ -180,7 +180,8 @@ The add commands return a pointer to an internally stored `Option`. If you set t
* `->required()`: The program will quit if this option is not present. This is `mandatory` in Plumbum, but required options seems to be a more standard term. For compatibility, `->mandatory()` also works.
* `->expected(N)`: Take `N` values instead of as many as possible, only for vector args. If negative, require at least `-N`; end with `--` or another recognized option.
* `->set_custom_option(typename, N)`: Set the name and (optional) intrinsic size of an option. The parser will require multiples of this number if negative.
* `->type_name(typename)`: Set the name of an Option's type (`type_name_fn` allows a function instead)
* `->type_size(N)`: Set the intrinsic size of an option. The parser will require multiples of this number if negative.
* `->needs(opt)`: This option requires another option to also be present, opt is an `Option` pointer.
* `->excludes(opt)`: This option cannot be given with `opt` present, opt is an `Option` pointer.
* `->envname(name)`: Gets the value from the environment if present and not passed on the command line.

View File

@ -17,7 +17,7 @@ int main(int argc, char **argv) {
Level level;
app.add_set("-l,--level", level, {Level::High, Level::Medium, Level::Low}, "Level settings")
->set_type_name("enum/Level in {High=0, Medium=1, Low=2}");
->type_name("enum/Level in {High=0, Medium=1, Low=2}");
CLI11_PARSE(app, argc, argv);

View File

@ -331,7 +331,7 @@ class App {
CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };
Option *opt = add_option(name, fun, description, false);
opt->set_custom_option(detail::type_name<T>());
opt->type_name(detail::type_name<T>());
return opt;
}
@ -345,11 +345,11 @@ class App {
CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };
Option *opt = add_option(name, fun, description, defaulted);
opt->set_custom_option(detail::type_name<T>());
opt->type_name(detail::type_name<T>());
if(defaulted) {
std::stringstream out;
out << variable;
opt->set_default_str(out.str());
opt->default_str(out.str());
}
return opt;
}
@ -371,7 +371,7 @@ class App {
};
Option *opt = add_option(name, fun, description, false);
opt->set_custom_option(detail::type_name<T>(), -1);
opt->type_name(detail::type_name<T>())->type_size(-1);
return opt;
}
@ -393,9 +393,9 @@ class App {
};
Option *opt = add_option(name, fun, description, defaulted);
opt->set_custom_option(detail::type_name<T>(), -1);
opt->type_name(detail::type_name<T>())->type_size(-1);
if(defaulted)
opt->set_default_str("[" + detail::join(variable) + "]");
opt->default_str("[" + detail::join(variable) + "]");
return opt;
}
@ -440,7 +440,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction::PositionalFlag(name);
opt->set_custom_option("", 0);
opt->type_size(0);
return opt;
}
@ -460,7 +460,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction::PositionalFlag(name);
opt->set_custom_option("", 0);
opt->type_size(0);
return opt;
}
@ -480,7 +480,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction::PositionalFlag(name);
opt->set_custom_option("", 0);
opt->type_size(0);
opt->multi_option_policy(CLI::MultiOptionPolicy::TakeLast);
return opt;
}
@ -499,7 +499,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction::PositionalFlag(name);
opt->set_custom_option("", 0);
opt->type_size(0);
return opt;
}
@ -530,7 +530,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
std::string typeval = detail::type_name<T>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
opt->type_name(typeval);
return opt;
}
@ -550,7 +550,7 @@ class App {
};
Option *opt = add_option(name, fun, description, false);
opt->set_type_name_fn(
opt->type_name_fn(
[&options]() { return std::string(detail::type_name<T>()) + " in {" + detail::join(options) + "}"; });
return opt;
@ -575,11 +575,11 @@ class App {
Option *opt = add_option(name, fun, description, defaulted);
std::string typeval = detail::type_name<T>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
opt->type_name(typeval);
if(defaulted) {
std::stringstream out;
out << member;
opt->set_default_str(out.str());
opt->default_str(out.str());
}
return opt;
}
@ -601,12 +601,12 @@ class App {
};
Option *opt = add_option(name, fun, description, defaulted);
opt->set_type_name_fn(
opt->type_name_fn(
[&options]() { return std::string(detail::type_name<T>()) + " in {" + detail::join(options) + "}"; });
if(defaulted) {
std::stringstream out;
out << member;
opt->set_default_str(out.str());
opt->default_str(out.str());
}
return opt;
}
@ -634,7 +634,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
std::string typeval = detail::type_name<std::string>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
opt->type_name(typeval);
return opt;
}
@ -660,7 +660,7 @@ class App {
};
Option *opt = add_option(name, fun, description, false);
opt->set_type_name_fn([&options]() {
opt->type_name_fn([&options]() {
return std::string(detail::type_name<std::string>()) + " in {" + detail::join(options) + "}";
});
@ -691,9 +691,9 @@ class App {
Option *opt = add_option(name, fun, description, defaulted);
std::string typeval = detail::type_name<std::string>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
opt->type_name(typeval);
if(defaulted) {
opt->set_default_str(member);
opt->default_str(member);
}
return opt;
}
@ -720,11 +720,11 @@ class App {
};
Option *opt = add_option(name, fun, description, defaulted);
opt->set_type_name_fn([&options]() {
opt->type_name_fn([&options]() {
return std::string(detail::type_name<std::string>()) + " in {" + detail::join(options) + "}";
});
if(defaulted) {
opt->set_default_str(member);
opt->default_str(member);
}
return opt;
}
@ -749,11 +749,11 @@ class App {
};
CLI::Option *opt = add_option(name, fun, description, defaulted);
opt->set_custom_option(label, 2);
opt->type_name(label)->type_size(2);
if(defaulted) {
std::stringstream out;
out << variable;
opt->set_default_str(out.str());
opt->default_str(out.str());
}
return opt;
}

View File

@ -174,7 +174,7 @@ class Option : public OptionBase<Option> {
/// A human readable type value, set when App creates this
///
/// This is a lambda function so "types" can be dynamic, such as when a set prints its contents.
std::function<std::string()> type_name_;
std::function<std::string()> type_name_{[]() { return std::string(); }};
/// True if this option has a default
bool default_{false};
@ -285,7 +285,7 @@ class Option : public OptionBase<Option> {
Option *check(const Validator &validator) {
validators_.emplace_back(validator.func);
if(!validator.tname.empty())
set_type_name(validator.tname);
type_name(validator.tname);
return this;
}
@ -630,15 +630,17 @@ class Option : public OptionBase<Option> {
}
/// Puts a result at the end
void add_result(std::string s) {
Option *add_result(std::string s) {
results_.push_back(s);
callback_run_ = false;
return this;
}
/// Set the results vector all at once
void set_results(std::vector<std::string> results) {
Option *set_results(std::vector<std::string> results) {
results_ = results;
callback_run_ = false;
return this;
}
/// Get a copy of the results
@ -651,36 +653,44 @@ class Option : public OptionBase<Option> {
/// @name Custom options
///@{
/// Set a custom option, typestring, type_size
void set_custom_option(std::string typeval, int type_size = 1) {
set_type_name(typeval);
/// Set the type function to run when displayed on this option
Option *type_name_fn(std::function<std::string()> typefun) {
type_name_ = typefun;
return this;
}
/// Set a custom option typestring
Option *type_name(std::string typeval) {
type_name_fn([typeval]() { return typeval; });
return this;
}
/// Set a custom option size
Option *type_size(int type_size) {
type_size_ = type_size;
if(type_size_ == 0)
required_ = false;
if(type_size < 0)
expected_ = -1;
return this;
}
/// Set the default value string representation
void set_default_str(std::string val) { defaultval_ = val; }
Option *default_str(std::string val) {
defaultval_ = val;
return this;
}
/// Set the default value string representation and evaluate
void set_default_val(std::string val) {
set_default_str(val);
Option *default_val(std::string val) {
default_str(val);
auto old_results = results_;
results_ = {val};
run_callback();
results_ = std::move(old_results);
return this;
}
/// Set the type name displayed on this option
void set_type_name(std::string typeval) {
set_type_name_fn([typeval]() { return typeval; });
}
/// Set the type function to run when displayed on this option
void set_type_name_fn(std::function<std::string()> typefun) { type_name_ = typefun; }
/// Get the typename for this option
std::string get_type_name() const { return type_name_(); }
};

View File

@ -1504,7 +1504,7 @@ TEST_F(TApp, CustomDoubleOption) {
custom_opt = {stol(vals.at(0)), stod(vals.at(1))};
return true;
});
opt->set_custom_option("INT FLOAT", 2);
opt->type_name("INT FLOAT")->type_size(2);
args = {"12", "1.5"};

View File

@ -222,8 +222,8 @@ TEST(THelp, ManualSetters) {
int x = 1;
CLI::Option *op1 = app.add_option("--op", x);
op1->set_default_str("12");
op1->set_type_name("BIGGLES");
op1->default_str("12");
op1->type_name("BIGGLES");
EXPECT_EQ(x, 1);
std::string help = app.help();
@ -231,7 +231,7 @@ TEST(THelp, ManualSetters) {
EXPECT_THAT(help, HasSubstr("=12"));
EXPECT_THAT(help, HasSubstr("BIGGLES"));
op1->set_default_val("14");
op1->default_val("14");
EXPECT_EQ(x, 14);
help = app.help();
EXPECT_THAT(help, HasSubstr("=14"));
@ -556,7 +556,7 @@ TEST(THelp, CustomDoubleOption) {
custom_opt = {stol(vals.at(0)), stod(vals.at(1))};
return true;
});
opt->set_custom_option("INT FLOAT", 2);
opt->type_name("INT FLOAT")->type_size(2);
EXPECT_THAT(app.help(), Not(HasSubstr("x 2")));
}

View File

@ -17,11 +17,11 @@ add_option(CLI::App &app, std::string name, cx &variable, std::string descriptio
};
CLI::Option *opt = app.add_option(name, fun, description, defaulted);
opt->set_custom_option("COMPLEX", 2);
opt->type_name("COMPLEX")->type_size(2);
if(defaulted) {
std::stringstream out;
out << variable;
opt->set_default_str(out.str());
opt->default_str(out.str());
}
return opt;
}