From 58b63617bd0a5a78bbb25d01f479679f38304bad Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Wed, 1 Oct 2014 17:52:34 +1000 Subject: [PATCH] add a string parser --- src/cxxopts.cpp | 3 +++ src/cxxopts.hpp | 15 +++++++++++++++ src/main.cpp | 17 ++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/cxxopts.cpp b/src/cxxopts.cpp index 7b65474..bacfc34 100644 --- a/src/cxxopts.cpp +++ b/src/cxxopts.cpp @@ -141,6 +141,7 @@ Options::parse(int& argc, char**& argv) if (i + 1 == s.size()) { checked_parse_arg(argc, argv, current+1, value, name); + ++current; } else { @@ -182,6 +183,8 @@ Options::parse(int& argc, char**& argv) { //parse the next argument checked_parse_arg(argc, argv, current + 1, opt, name); + + ++current; } else { diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index c900692..8e2dd00 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -35,6 +35,21 @@ namespace cxxopts return false; } }; + + class String : public Value + { + void + parse(const std::string& text, any& result) const + { + result = text; + } + + bool + has_arg() const + { + return true; + } + }; } extern std::basic_regex option_matcher; diff --git a/src/main.cpp b/src/main.cpp index df6a4cb..160df82 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,11 +31,26 @@ int main(int argc, char* argv[]) options.add_options() ("a,apple", "an apple") ("b,bob", "Bob") - ("b,bob", "Bob") + ("f,file", "File", std::make_shared()) ; options.parse(argc, argv); + if (options.count("a")) + { + std::cout << "Saw option ‘a’" << std::endl; + } + + if (options.count("b")) + { + std::cout << "Saw option ‘b’" << std::endl; + } + + if (options.count("f")) + { + std::cout << "File = " << boost::any_cast(options["f"]) << std::endl; + } + } catch (const std::regex_error& e) { std::cout << "regex_error: " << e.what() << std::endl;