Commit 70d458e90bd9bc2f2325c1e1df7d3e6c02c3b87a
1 parent
58b63617
default_value
Showing
2 changed files
with
36 additions
and
5 deletions
src/cxxopts.hpp
| ... | ... | @@ -3,6 +3,7 @@ |
| 3 | 3 | #include <map> |
| 4 | 4 | #include <exception> |
| 5 | 5 | #include <boost/any.hpp> |
| 6 | +#include <sstream> | |
| 6 | 7 | |
| 7 | 8 | namespace cxxopts |
| 8 | 9 | { |
| ... | ... | @@ -21,7 +22,27 @@ namespace cxxopts |
| 21 | 22 | |
| 22 | 23 | namespace values |
| 23 | 24 | { |
| 24 | - class Boolean : public Value | |
| 25 | + template <typename T> | |
| 26 | + class default_value : public Value | |
| 27 | + { | |
| 28 | + void | |
| 29 | + parse(const std::string& text, any& result) const | |
| 30 | + { | |
| 31 | + T t; | |
| 32 | + std::istringstream is(text); | |
| 33 | + is >> t; | |
| 34 | + result = t; | |
| 35 | + } | |
| 36 | + | |
| 37 | + bool | |
| 38 | + has_arg() const | |
| 39 | + { | |
| 40 | + return true; | |
| 41 | + } | |
| 42 | + }; | |
| 43 | + | |
| 44 | + template <> | |
| 45 | + class default_value<bool> : public Value | |
| 25 | 46 | { |
| 26 | 47 | void |
| 27 | 48 | parse(const std::string& text, any& result) const |
| ... | ... | @@ -36,7 +57,8 @@ namespace cxxopts |
| 36 | 57 | } |
| 37 | 58 | }; |
| 38 | 59 | |
| 39 | - class String : public Value | |
| 60 | + template <> | |
| 61 | + class default_value<std::string> : public Value | |
| 40 | 62 | { |
| 41 | 63 | void |
| 42 | 64 | parse(const std::string& text, any& result) const |
| ... | ... | @@ -52,6 +74,13 @@ namespace cxxopts |
| 52 | 74 | }; |
| 53 | 75 | } |
| 54 | 76 | |
| 77 | + template <typename T> | |
| 78 | + std::shared_ptr<Value> | |
| 79 | + value() | |
| 80 | + { | |
| 81 | + return std::make_shared<values::default_value<T>>(); | |
| 82 | + } | |
| 83 | + | |
| 55 | 84 | extern std::basic_regex<char> option_matcher; |
| 56 | 85 | |
| 57 | 86 | extern std::basic_regex<char> option_specifier; |
| ... | ... | @@ -293,10 +322,11 @@ namespace cxxopts |
| 293 | 322 | const std::string& opts, |
| 294 | 323 | const std::string& desc, |
| 295 | 324 | std::shared_ptr<const Value> value |
| 296 | - = std::make_shared<values::Boolean>() | |
| 325 | + = ::cxxopts::value<bool>() | |
| 297 | 326 | ); |
| 298 | 327 | |
| 299 | 328 | private: |
| 300 | 329 | Options& m_options; |
| 301 | 330 | }; |
| 331 | + | |
| 302 | 332 | } | ... | ... |
src/main.cpp
| ... | ... | @@ -31,7 +31,7 @@ int main(int argc, char* argv[]) |
| 31 | 31 | options.add_options() |
| 32 | 32 | ("a,apple", "an apple") |
| 33 | 33 | ("b,bob", "Bob") |
| 34 | - ("f,file", "File", std::make_shared<cxxopts::values::String>()) | |
| 34 | + ("f,file", "File", cxxopts::value<std::string>()) | |
| 35 | 35 | ; |
| 36 | 36 | |
| 37 | 37 | options.parse(argc, argv); |
| ... | ... | @@ -48,7 +48,8 @@ int main(int argc, char* argv[]) |
| 48 | 48 | |
| 49 | 49 | if (options.count("f")) |
| 50 | 50 | { |
| 51 | - std::cout << "File = " << boost::any_cast<std::string>(options["f"]) << std::endl; | |
| 51 | + std::cout << "File = " << boost::any_cast<std::string>(options["f"]) | |
| 52 | + << std::endl; | |
| 52 | 53 | } |
| 53 | 54 | |
| 54 | 55 | } catch (const std::regex_error& e) | ... | ... |