1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-30 20:53:52 +00:00

Making it easier to add custom options

This commit is contained in:
Henry Fredrick Schreiner 2017-03-23 09:59:02 -04:00
parent 4d61a4530c
commit e8c195b683
2 changed files with 37 additions and 20 deletions

View File

@ -237,11 +237,11 @@ public:
};
Option* opt = add_option(name, fun, description, defaulted);
opt->typeval_ = detail::type_name<T>();
opt->set_custom_option(detail::type_name<T>());
if(defaulted) {
std::stringstream out;
out << variable;
opt->defaultval_ = out.str();
opt->set_default_val(out.str());
}
return opt;
}
@ -266,11 +266,9 @@ public:
};
Option* opt = add_option(name, fun, description, defaulted);
opt->allow_vector_ = true;
opt->expected_ = -1;
opt->typeval_ = detail::type_name<T>();
opt->set_custom_option(detail::type_name<T>(), -1, true);
if(defaulted)
opt->defaultval_ = "[" + detail::join(variable) + "]";
opt->set_default_val("[" + detail::join(variable) + "]");
return opt;
}
@ -286,7 +284,7 @@ public:
Option* opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction("Flags cannot be positional");
opt->expected_ = 0;
opt->set_custom_option("", 0);
return opt;
}
@ -308,7 +306,7 @@ public:
Option* opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction("Flags cannot be positional");
opt->expected_ = 0;
opt->set_custom_option("", 0);
return opt;
}
@ -330,7 +328,7 @@ public:
Option* opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction("Flags cannot be positional");
opt->expected_ = 0;
opt->set_custom_option("", 0);
return opt;
}
@ -356,12 +354,13 @@ public:
};
Option* opt = add_option(name, fun, description, defaulted);
opt->typeval_ = detail::type_name<T>();
opt->typeval_ += " in {" + detail::join(options) + "}";
std::string typeval = detail::type_name<T>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
if(defaulted) {
std::stringstream out;
out << member;
opt->defaultval_ = out.str();
opt->set_default_val(out.str());
}
return opt;
}
@ -391,10 +390,11 @@ public:
};
Option* opt = add_option(name, fun, description, defaulted);
opt->typeval_ = detail::type_name<std::string>();
opt->typeval_ += " in {" + detail::join(options) + "}";
std::string typeval = detail::type_name<std::string>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
if(defaulted) {
opt->defaultval_ = member;
opt->set_default_val(member);
}
return opt;
}

View File

@ -73,8 +73,8 @@ protected:
/// The number of expected values, 0 for flag, -1 for unlimited vector
int expected_ {1};
/// A private setting to allow non-vector args to not be able to accept incorrect expected values
bool allow_vector_ {false};
/// A private setting to allow args to not be able to accept incorrect expected values
bool changeable_ {false};
/// Ignore the case when matching (option, not value)
bool ignore_case_ {false};
@ -155,12 +155,12 @@ public:
/// Set the number of expected arguments (Flags bypass this)
Option* expected(int value) {
if(value == 0)
if(!changeable_)
throw IncorrectConstruction("You can only change the expected arguments for vectors");
else if(value == 0)
throw IncorrectConstruction("Cannot set 0 expected, use a flag instead");
else if(expected_ == 0)
throw IncorrectConstruction("Cannot make a flag take arguments!");
else if(!allow_vector_ && value != 1)
throw IncorrectConstruction("You can only change the Expected arguments for vectors");
expected_ = value;
return this;
}
@ -460,6 +460,23 @@ public:
}
///@}
/// @name Custom options
///@{
/// Set a custom option, typestring, expected, and changeable
void set_custom_option(std::string typeval, int expected=1, bool changeable = false) {
typeval_ = typeval;
expected_ = expected;
changeable_ = changeable;
}
/// Set the default value string representation
void set_default_val(std::string val) {
defaultval_ = val;
}
///@}
protected: