1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-01 21:23: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); Option* opt = add_option(name, fun, description, defaulted);
opt->typeval_ = detail::type_name<T>(); opt->set_custom_option(detail::type_name<T>());
if(defaulted) { if(defaulted) {
std::stringstream out; std::stringstream out;
out << variable; out << variable;
opt->defaultval_ = out.str(); opt->set_default_val(out.str());
} }
return opt; return opt;
} }
@ -266,11 +266,9 @@ public:
}; };
Option* opt = add_option(name, fun, description, defaulted); Option* opt = add_option(name, fun, description, defaulted);
opt->allow_vector_ = true; opt->set_custom_option(detail::type_name<T>(), -1, true);
opt->expected_ = -1;
opt->typeval_ = detail::type_name<T>();
if(defaulted) if(defaulted)
opt->defaultval_ = "[" + detail::join(variable) + "]"; opt->set_default_val("[" + detail::join(variable) + "]");
return opt; return opt;
} }
@ -286,7 +284,7 @@ public:
Option* opt = add_option(name, fun, description, false); Option* opt = add_option(name, fun, description, false);
if(opt->get_positional()) if(opt->get_positional())
throw IncorrectConstruction("Flags cannot be positional"); throw IncorrectConstruction("Flags cannot be positional");
opt->expected_ = 0; opt->set_custom_option("", 0);
return opt; return opt;
} }
@ -308,7 +306,7 @@ public:
Option* opt = add_option(name, fun, description, false); Option* opt = add_option(name, fun, description, false);
if(opt->get_positional()) if(opt->get_positional())
throw IncorrectConstruction("Flags cannot be positional"); throw IncorrectConstruction("Flags cannot be positional");
opt->expected_ = 0; opt->set_custom_option("", 0);
return opt; return opt;
} }
@ -330,7 +328,7 @@ public:
Option* opt = add_option(name, fun, description, false); Option* opt = add_option(name, fun, description, false);
if(opt->get_positional()) if(opt->get_positional())
throw IncorrectConstruction("Flags cannot be positional"); throw IncorrectConstruction("Flags cannot be positional");
opt->expected_ = 0; opt->set_custom_option("", 0);
return opt; return opt;
} }
@ -356,12 +354,13 @@ public:
}; };
Option* opt = add_option(name, fun, description, defaulted); Option* opt = add_option(name, fun, description, defaulted);
opt->typeval_ = detail::type_name<T>(); std::string typeval = detail::type_name<T>();
opt->typeval_ += " in {" + detail::join(options) + "}"; typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
if(defaulted) { if(defaulted) {
std::stringstream out; std::stringstream out;
out << member; out << member;
opt->defaultval_ = out.str(); opt->set_default_val(out.str());
} }
return opt; return opt;
} }
@ -391,10 +390,11 @@ public:
}; };
Option* opt = add_option(name, fun, description, defaulted); Option* opt = add_option(name, fun, description, defaulted);
opt->typeval_ = detail::type_name<std::string>(); std::string typeval = detail::type_name<std::string>();
opt->typeval_ += " in {" + detail::join(options) + "}"; typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
if(defaulted) { if(defaulted) {
opt->defaultval_ = member; opt->set_default_val(member);
} }
return opt; return opt;
} }

View File

@ -73,8 +73,8 @@ protected:
/// The number of expected values, 0 for flag, -1 for unlimited vector /// The number of expected values, 0 for flag, -1 for unlimited vector
int expected_ {1}; int expected_ {1};
/// A private setting to allow non-vector args to not be able to accept incorrect expected values /// A private setting to allow args to not be able to accept incorrect expected values
bool allow_vector_ {false}; bool changeable_ {false};
/// Ignore the case when matching (option, not value) /// Ignore the case when matching (option, not value)
bool ignore_case_ {false}; bool ignore_case_ {false};
@ -155,12 +155,12 @@ public:
/// Set the number of expected arguments (Flags bypass this) /// Set the number of expected arguments (Flags bypass this)
Option* expected(int value) { 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"); throw IncorrectConstruction("Cannot set 0 expected, use a flag instead");
else if(expected_ == 0) else if(expected_ == 0)
throw IncorrectConstruction("Cannot make a flag take arguments!"); 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; expected_ = value;
return this; 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: protected: