diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index 8e2dd00..93048dc 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace cxxopts { @@ -21,7 +22,27 @@ namespace cxxopts namespace values { - class Boolean : public Value + template + class default_value : public Value + { + void + parse(const std::string& text, any& result) const + { + T t; + std::istringstream is(text); + is >> t; + result = t; + } + + bool + has_arg() const + { + return true; + } + }; + + template <> + class default_value : public Value { void parse(const std::string& text, any& result) const @@ -36,7 +57,8 @@ namespace cxxopts } }; - class String : public Value + template <> + class default_value : public Value { void parse(const std::string& text, any& result) const @@ -52,6 +74,13 @@ namespace cxxopts }; } + template + std::shared_ptr + value() + { + return std::make_shared>(); + } + extern std::basic_regex option_matcher; extern std::basic_regex option_specifier; @@ -293,10 +322,11 @@ namespace cxxopts const std::string& opts, const std::string& desc, std::shared_ptr value - = std::make_shared() + = ::cxxopts::value() ); private: Options& m_options; }; + } diff --git a/src/main.cpp b/src/main.cpp index 160df82..52bac6a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char* argv[]) options.add_options() ("a,apple", "an apple") ("b,bob", "Bob") - ("f,file", "File", std::make_shared()) + ("f,file", "File", cxxopts::value()) ; options.parse(argc, argv); @@ -48,7 +48,8 @@ int main(int argc, char* argv[]) if (options.count("f")) { - std::cout << "File = " << boost::any_cast(options["f"]) << std::endl; + std::cout << "File = " << boost::any_cast(options["f"]) + << std::endl; } } catch (const std::regex_error& e)