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

Actually support multiple options

This commit is contained in:
Henry Fredrick Schreiner 2017-02-04 08:29:34 -05:00
parent 2fae7e2cdf
commit 9dea0cf2e9
2 changed files with 39 additions and 9 deletions

View File

@ -208,9 +208,6 @@ namespace detail {
self.validators.push_back(func); self.validators.push_back(func);
return self; return self;
} }
Combiner operator, (Combiner b) const {
return *this | b;
}
}; };
bool _ExistingFile(std::string filename) { bool _ExistingFile(std::string filename) {
@ -573,17 +570,20 @@ protected:
std::string name; std::string name;
public: public:
Value(std::string name) : name(name) {} Value(std::string name) : name(name) {}
operator bool() const {return (bool) *value;} operator bool() const {return (bool) *value;}
T& get() const {
if(*value)
return **value;
else
throw EmptyError(name);
}
/// Note this does not throw on assignment, though /// Note this does not throw on assignment, though
/// afterwards it seems to work fine. Best to use /// afterwards it seems to work fine. Best to use
/// explicit * notation. /// explicit * notation.
T& operator *() const { T& operator *() const {
if(*value) { return get();
return **value;
}
else {
throw EmptyError(name);
}
} }
}; };
@ -745,6 +745,18 @@ public:
} }
/// Multiple options are supported
template<typename T, typename... Args>
Option* add_option(
std::string name, ///< The name, long,short
T &variable, ///< The variable to set
std::string discription, ///< Discription string
detail::Combiner opts, ///< The options (REQUIRED, DEFAULT, POSITIONAL, ARGS())
detail::Combiner opts2,
Args... args ///< More options
) {
return add_option(name, variable, discription, opts|opts2, args...);
}
/// Add option for flag /// Add option for flag
Option* add_flag( Option* add_flag(
std::string name, ///< The name, short,long std::string name, ///< The name, short,long

View File

@ -190,6 +190,24 @@ TEST_F(TApp, ShortOpts) {
EXPECT_EQ("zyz", someopt); EXPECT_EQ("zyz", someopt);
} }
TEST_F(TApp, Flags) {
int i = 3;
std::string s = "HI";
app.add_option("-i", i, "", CLI::DEFAULT, CLI::POSITIONAL);
app.add_option("-s", s, "", CLI::DEFAULT, CLI::POSITIONAL);
args = {"-i2", "9"};
EXPECT_NO_THROW(run());
EXPECT_EQ(1, app.count("i"));
EXPECT_EQ(1, app.count("s"));
EXPECT_EQ(2, i);
EXPECT_EQ("9", s);
}
TEST_F(TApp, Positionals) { TEST_F(TApp, Positionals) {
std::string posit1; std::string posit1;