diff --git a/include/CLI.hpp b/include/CLI.hpp index b546068d..7d0d1792 100644 --- a/include/CLI.hpp +++ b/include/CLI.hpp @@ -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 + 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 diff --git a/tests/CLITest.cpp b/tests/CLITest.cpp index ac830fa6..4f98e652 100644 --- a/tests/CLITest.cpp +++ b/tests/CLITest.cpp @@ -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;