Commit 9dea0cf2e9886b43febdf88eaf097b2a21dc65f5
1 parent
2fae7e2c
Actually support multiple options
Showing
2 changed files
with
39 additions
and
9 deletions
include/CLI.hpp
| ... | ... | @@ -208,9 +208,6 @@ namespace detail { |
| 208 | 208 | self.validators.push_back(func); |
| 209 | 209 | return self; |
| 210 | 210 | } |
| 211 | - Combiner operator, (Combiner b) const { | |
| 212 | - return *this | b; | |
| 213 | - } | |
| 214 | 211 | }; |
| 215 | 212 | |
| 216 | 213 | bool _ExistingFile(std::string filename) { |
| ... | ... | @@ -573,17 +570,20 @@ protected: |
| 573 | 570 | std::string name; |
| 574 | 571 | public: |
| 575 | 572 | Value(std::string name) : name(name) {} |
| 573 | + | |
| 576 | 574 | operator bool() const {return (bool) *value;} |
| 575 | + | |
| 576 | + T& get() const { | |
| 577 | + if(*value) | |
| 578 | + return **value; | |
| 579 | + else | |
| 580 | + throw EmptyError(name); | |
| 581 | + } | |
| 577 | 582 | /// Note this does not throw on assignment, though |
| 578 | 583 | /// afterwards it seems to work fine. Best to use |
| 579 | 584 | /// explicit * notation. |
| 580 | 585 | T& operator *() const { |
| 581 | - if(*value) { | |
| 582 | - return **value; | |
| 583 | - } | |
| 584 | - else { | |
| 585 | - throw EmptyError(name); | |
| 586 | - } | |
| 586 | + return get(); | |
| 587 | 587 | } |
| 588 | 588 | }; |
| 589 | 589 | |
| ... | ... | @@ -745,6 +745,18 @@ public: |
| 745 | 745 | } |
| 746 | 746 | |
| 747 | 747 | |
| 748 | + /// Multiple options are supported | |
| 749 | + template<typename T, typename... Args> | |
| 750 | + Option* add_option( | |
| 751 | + std::string name, ///< The name, long,short | |
| 752 | + T &variable, ///< The variable to set | |
| 753 | + std::string discription, ///< Discription string | |
| 754 | + detail::Combiner opts, ///< The options (REQUIRED, DEFAULT, POSITIONAL, ARGS()) | |
| 755 | + detail::Combiner opts2, | |
| 756 | + Args... args ///< More options | |
| 757 | + ) { | |
| 758 | + return add_option(name, variable, discription, opts|opts2, args...); | |
| 759 | + } | |
| 748 | 760 | /// Add option for flag |
| 749 | 761 | Option* add_flag( |
| 750 | 762 | std::string name, ///< The name, short,long | ... | ... |
tests/CLITest.cpp
| ... | ... | @@ -190,6 +190,24 @@ TEST_F(TApp, ShortOpts) { |
| 190 | 190 | EXPECT_EQ("zyz", someopt); |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | +TEST_F(TApp, Flags) { | |
| 194 | + | |
| 195 | + int i = 3; | |
| 196 | + std::string s = "HI"; | |
| 197 | + | |
| 198 | + app.add_option("-i", i, "", CLI::DEFAULT, CLI::POSITIONAL); | |
| 199 | + app.add_option("-s", s, "", CLI::DEFAULT, CLI::POSITIONAL); | |
| 200 | + | |
| 201 | + args = {"-i2", "9"}; | |
| 202 | + | |
| 203 | + EXPECT_NO_THROW(run()); | |
| 204 | + | |
| 205 | + EXPECT_EQ(1, app.count("i")); | |
| 206 | + EXPECT_EQ(1, app.count("s")); | |
| 207 | + EXPECT_EQ(2, i); | |
| 208 | + EXPECT_EQ("9", s); | |
| 209 | +} | |
| 210 | + | |
| 193 | 211 | TEST_F(TApp, Positionals) { |
| 194 | 212 | |
| 195 | 213 | std::string posit1; | ... | ... |