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:
parent
2fae7e2cdf
commit
9dea0cf2e9
@ -208,9 +208,6 @@ namespace detail {
|
||||
self.validators.push_back(func);
|
||||
return self;
|
||||
}
|
||||
Combiner operator, (Combiner b) const {
|
||||
return *this | b;
|
||||
}
|
||||
};
|
||||
|
||||
bool _ExistingFile(std::string filename) {
|
||||
@ -573,17 +570,20 @@ protected:
|
||||
std::string name;
|
||||
public:
|
||||
Value(std::string name) : name(name) {}
|
||||
|
||||
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
|
||||
/// afterwards it seems to work fine. Best to use
|
||||
/// explicit * notation.
|
||||
T& operator *() const {
|
||||
if(*value) {
|
||||
return **value;
|
||||
}
|
||||
else {
|
||||
throw EmptyError(name);
|
||||
}
|
||||
return get();
|
||||
}
|
||||
};
|
||||
|
||||
@ -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
|
||||
Option* add_flag(
|
||||
std::string name, ///< The name, short,long
|
||||
|
@ -190,6 +190,24 @@ TEST_F(TApp, ShortOpts) {
|
||||
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) {
|
||||
|
||||
std::string posit1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user