From a61dd78a57a59a766a32915ea12eb46014d21095 Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Sat, 27 Sep 2014 22:04:53 +1000 Subject: [PATCH] parsing --- src/cxxopts.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ src/cxxopts.hpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/main.cpp | 2 ++ 3 files changed, 126 insertions(+), 9 deletions(-) diff --git a/src/cxxopts.cpp b/src/cxxopts.cpp index f505504..8e502fa 100644 --- a/src/cxxopts.cpp +++ b/src/cxxopts.cpp @@ -19,7 +19,8 @@ OptionAdder& OptionAdder::operator() ( const std::string& opts, - const std::string& desc + const std::string& desc, + std::shared_ptr value ) { std::match_results result; @@ -33,13 +34,13 @@ OptionAdder::operator() const auto& s = result[2]; const auto& l = result[3]; - auto option = std::make_shared(desc); + auto option = std::make_shared(desc, value); if (s.length() != 0) { - auto result = m_options.m_short.insert(std::make_pair(s.str()[0], option)); + auto in = m_options.m_short.insert(std::make_pair(s.str(), option)); - if (!result.second) + if (!in.second) { throw option_exists_error(s.str()); } @@ -47,9 +48,9 @@ OptionAdder::operator() if (l.length() != 0) { - auto result = m_options.m_long.insert(std::make_pair(l, option)); + auto in = m_options.m_long.insert(std::make_pair(l, option)); - if (!result.second) + if (!in.second) { throw option_exists_error(l.str()); } @@ -58,4 +59,66 @@ OptionAdder::operator() return *this; } +void +Options::parse(int& argc, char**& argv) +{ + int current = 1; + + std::set consumed; + + while (current != argc) + { + std::match_results result; + std::regex_match(argv[current], result, option_matcher); + + if (result.empty()) + { + //handle empty + + //if we return from here then it was parsed successfully, so continue + } + else + { + //short or long option? + if (result[4].length() != 0) + { + std::string s = result[4]; + + for (int i = 0; i != s.size(); ++i) + { + auto iter = m_short.find(std::string(1, s[i])); + + if (iter == m_short.end()) + { + } + + auto value = iter->second; + + if (value->has_arg()) + { + } + } + } + else if (result[1].length() != 0) + { + if (result[3].length() != 0) + { + auto iter = m_long.find(result[3]); + + if (iter == m_long.end()) + { + } + + auto value = iter->second; + } + + //equals provided for long option? + } + + } + + ++current; + } +} + } diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index 8e64f4c..aa89386 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -2,9 +2,40 @@ #include #include #include +#include namespace cxxopts { + using boost::any; + + class Value + { + public: + + virtual void + parse(const std::string& text, any& result) const = 0; + + virtual bool + has_arg() const = 0; + }; + + namespace values + { + class Boolean : public Value + { + void + parse(const std::string& text, any& result) const + { + } + + bool + has_arg() const + { + return false; + } + }; + } + extern std::basic_regex option_matcher; extern std::basic_regex option_specifier; @@ -54,13 +85,32 @@ namespace cxxopts class OptionDetails { public: - OptionDetails(const std::string& description) + OptionDetails + ( + const std::string& description, + std::shared_ptr value + ) : m_desc(description) + , m_parser(value) + { + } + + const std::string& + description() const + { + return m_desc; + } + + bool + has_arg() const { + return m_parser->has_arg(); } private: + boost::any m_value; std::string m_desc; + std::shared_ptr m_parser; }; class Options @@ -76,7 +126,7 @@ namespace cxxopts private: friend class OptionAdder; - std::map> m_short; + std::map> m_short; std::map> m_long; }; @@ -93,7 +143,9 @@ namespace cxxopts operator() ( const std::string& opts, - const std::string& desc + const std::string& desc, + std::shared_ptr value + = std::make_shared() ); private: diff --git a/src/main.cpp b/src/main.cpp index 4505075..1f58c9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,8 @@ int main(int argc, char* argv[]) ("b,bob", "Bob") ; + options.parse(argc, argv); + } catch (const std::regex_error& e) { std::cout << "regex_error: " << e.what() << std::endl; -- libgit2 0.21.4