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

Adding functional form of Type printout

This commit is contained in:
Henry Fredrick Schreiner 2018-05-03 10:47:42 +02:00 committed by Henry Schreiner
parent 2b6b62c52c
commit e7483afc41
4 changed files with 43 additions and 21 deletions

View File

@ -538,9 +538,9 @@ 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->set_type_name(
[&options]() { return std::string(detail::type_name<T>()) + " in {" + detail::join(options) + "}"; });
return opt;
}
@ -589,9 +589,8 @@ 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->set_type_name(
[&options]() { return std::string(detail::type_name<T>()) + " in {" + detail::join(options) + "}"; });
if(defaulted) {
std::stringstream out;
out << member;
@ -649,9 +648,9 @@ 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->set_type_name([&options]() {
return std::string(detail::type_name<std::string>()) + " in {" + detail::join(options) + "}";
});
return opt;
}
@ -709,9 +708,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->set_type_name([&options]() {
return std::string(detail::type_name<std::string>()) + " in {" + detail::join(options) + "}";
});
if(defaulted) {
opt->set_default_str(member);
}

View File

@ -193,8 +193,8 @@ inline std::string Formatter::make_option_opts(const Option *opt) const {
std::stringstream out;
if(opt->get_type_size() != 0) {
if(!opt->get_typeval().empty())
out << " " << get_label(opt->get_typeval());
if(!opt->get_type_name().empty())
out << " " << get_label(opt->get_type_name());
if(!opt->get_defaultval().empty())
out << "=" << opt->get_defaultval();
if(opt->get_expected() > 1)

View File

@ -171,7 +171,7 @@ class Option : public OptionBase<Option> {
std::string defaultval_;
/// A human readable type value, set when App creates this
std::string typeval_;
std::function<std::string()> typeval_;
/// True if this option has a default
bool default_{false};
@ -385,9 +385,6 @@ class Option : public OptionBase<Option> {
/// The number of arguments the option expects
int get_type_size() const { return type_size_; }
/// The type name (for help printing)
std::string get_typeval() const { return typeval_; }
/// The environment variable associated to this value
std::string get_envname() const { return envname_; }
@ -612,7 +609,7 @@ class Option : public OptionBase<Option> {
/// Set a custom option, typestring, type_size
void set_custom_option(std::string typeval, int type_size = 1) {
typeval_ = typeval;
set_type_name(typeval);
type_size_ = type_size;
if(type_size_ == 0)
required_ = false;
@ -633,10 +630,15 @@ class Option : public OptionBase<Option> {
}
/// Set the type name displayed on this option
void set_type_name(std::string val) { typeval_ = val; }
void set_type_name(std::string typeval) {
typeval_ = [typeval]() { return typeval; };
}
/// Set the type function to run when displayed on this option
void set_type_name(std::function<std::string()> typefun) { typeval_ = typefun; }
/// Get the typename for this option
std::string get_type_name() const { return typeval_; }
std::string get_type_name() const { return typeval_(); }
};
} // namespace CLI

View File

@ -665,4 +665,25 @@ TEST(THelp, CombinedValidatorsPathyText) {
std::string help = app.help();
EXPECT_THAT(help, Not(HasSubstr("TEXT")));
EXPECT_THAT(help, HasSubstr("PATH"));
// #113 Part 2
TEST(THelp, ChangingSet) {
CLI::App app;
std::set<int> vals{1, 2, 3};
int val;
app.add_set("--val", val, vals);
std::string help = app.help();
EXPECT_THAT(help, HasSubstr("1"));
EXPECT_THAT(help, Not(HasSubstr("4")));
vals.insert(4);
vals.erase(1);
help = app.help();
EXPECT_THAT(help, Not(HasSubstr("1")));
EXPECT_THAT(help, HasSubstr("4"));
}