Commit a61dd78a57a59a766a32915ea12eb46014d21095
1 parent
5cd3fb22
parsing
Showing
3 changed files
with
126 additions
and
9 deletions
src/cxxopts.cpp
| ... | ... | @@ -19,7 +19,8 @@ OptionAdder& |
| 19 | 19 | OptionAdder::operator() |
| 20 | 20 | ( |
| 21 | 21 | const std::string& opts, |
| 22 | - const std::string& desc | |
| 22 | + const std::string& desc, | |
| 23 | + std::shared_ptr<const Value> value | |
| 23 | 24 | ) |
| 24 | 25 | { |
| 25 | 26 | std::match_results<const char*> result; |
| ... | ... | @@ -33,13 +34,13 @@ OptionAdder::operator() |
| 33 | 34 | const auto& s = result[2]; |
| 34 | 35 | const auto& l = result[3]; |
| 35 | 36 | |
| 36 | - auto option = std::make_shared<OptionDetails>(desc); | |
| 37 | + auto option = std::make_shared<OptionDetails>(desc, value); | |
| 37 | 38 | |
| 38 | 39 | if (s.length() != 0) |
| 39 | 40 | { |
| 40 | - auto result = m_options.m_short.insert(std::make_pair(s.str()[0], option)); | |
| 41 | + auto in = m_options.m_short.insert(std::make_pair(s.str(), option)); | |
| 41 | 42 | |
| 42 | - if (!result.second) | |
| 43 | + if (!in.second) | |
| 43 | 44 | { |
| 44 | 45 | throw option_exists_error(s.str()); |
| 45 | 46 | } |
| ... | ... | @@ -47,9 +48,9 @@ OptionAdder::operator() |
| 47 | 48 | |
| 48 | 49 | if (l.length() != 0) |
| 49 | 50 | { |
| 50 | - auto result = m_options.m_long.insert(std::make_pair(l, option)); | |
| 51 | + auto in = m_options.m_long.insert(std::make_pair(l, option)); | |
| 51 | 52 | |
| 52 | - if (!result.second) | |
| 53 | + if (!in.second) | |
| 53 | 54 | { |
| 54 | 55 | throw option_exists_error(l.str()); |
| 55 | 56 | } |
| ... | ... | @@ -58,4 +59,66 @@ OptionAdder::operator() |
| 58 | 59 | return *this; |
| 59 | 60 | } |
| 60 | 61 | |
| 62 | +void | |
| 63 | +Options::parse(int& argc, char**& argv) | |
| 64 | +{ | |
| 65 | + int current = 1; | |
| 66 | + | |
| 67 | + std::set<int> consumed; | |
| 68 | + | |
| 69 | + while (current != argc) | |
| 70 | + { | |
| 71 | + std::match_results<const char*> result; | |
| 72 | + std::regex_match(argv[current], result, option_matcher); | |
| 73 | + | |
| 74 | + if (result.empty()) | |
| 75 | + { | |
| 76 | + //handle empty | |
| 77 | + | |
| 78 | + //if we return from here then it was parsed successfully, so continue | |
| 79 | + } | |
| 80 | + else | |
| 81 | + { | |
| 82 | + //short or long option? | |
| 83 | + if (result[4].length() != 0) | |
| 84 | + { | |
| 85 | + std::string s = result[4]; | |
| 86 | + | |
| 87 | + for (int i = 0; i != s.size(); ++i) | |
| 88 | + { | |
| 89 | + auto iter = m_short.find(std::string(1, s[i])); | |
| 90 | + | |
| 91 | + if (iter == m_short.end()) | |
| 92 | + { | |
| 93 | + } | |
| 94 | + | |
| 95 | + auto value = iter->second; | |
| 96 | + | |
| 97 | + if (value->has_arg()) | |
| 98 | + { | |
| 99 | + } | |
| 100 | + } | |
| 101 | + } | |
| 102 | + else if (result[1].length() != 0) | |
| 103 | + { | |
| 104 | + if (result[3].length() != 0) | |
| 105 | + { | |
| 106 | + auto iter = m_long.find(result[3]); | |
| 107 | + | |
| 108 | + if (iter == m_long.end()) | |
| 109 | + { | |
| 110 | + } | |
| 111 | + | |
| 112 | + auto value = iter->second; | |
| 113 | + } | |
| 114 | + | |
| 115 | + //equals provided for long option? | |
| 116 | + } | |
| 117 | + | |
| 118 | + } | |
| 119 | + | |
| 120 | + ++current; | |
| 121 | + } | |
| 122 | +} | |
| 123 | + | |
| 61 | 124 | } | ... | ... |
src/cxxopts.hpp
| ... | ... | @@ -2,9 +2,40 @@ |
| 2 | 2 | #include <set> |
| 3 | 3 | #include <map> |
| 4 | 4 | #include <exception> |
| 5 | +#include <boost/any.hpp> | |
| 5 | 6 | |
| 6 | 7 | namespace cxxopts |
| 7 | 8 | { |
| 9 | + using boost::any; | |
| 10 | + | |
| 11 | + class Value | |
| 12 | + { | |
| 13 | + public: | |
| 14 | + | |
| 15 | + virtual void | |
| 16 | + parse(const std::string& text, any& result) const = 0; | |
| 17 | + | |
| 18 | + virtual bool | |
| 19 | + has_arg() const = 0; | |
| 20 | + }; | |
| 21 | + | |
| 22 | + namespace values | |
| 23 | + { | |
| 24 | + class Boolean : public Value | |
| 25 | + { | |
| 26 | + void | |
| 27 | + parse(const std::string& text, any& result) const | |
| 28 | + { | |
| 29 | + } | |
| 30 | + | |
| 31 | + bool | |
| 32 | + has_arg() const | |
| 33 | + { | |
| 34 | + return false; | |
| 35 | + } | |
| 36 | + }; | |
| 37 | + } | |
| 38 | + | |
| 8 | 39 | extern std::basic_regex<char> option_matcher; |
| 9 | 40 | |
| 10 | 41 | extern std::basic_regex<char> option_specifier; |
| ... | ... | @@ -54,13 +85,32 @@ namespace cxxopts |
| 54 | 85 | class OptionDetails |
| 55 | 86 | { |
| 56 | 87 | public: |
| 57 | - OptionDetails(const std::string& description) | |
| 88 | + OptionDetails | |
| 89 | + ( | |
| 90 | + const std::string& description, | |
| 91 | + std::shared_ptr<const Value> value | |
| 92 | + ) | |
| 58 | 93 | : m_desc(description) |
| 94 | + , m_parser(value) | |
| 95 | + { | |
| 96 | + } | |
| 97 | + | |
| 98 | + const std::string& | |
| 99 | + description() const | |
| 100 | + { | |
| 101 | + return m_desc; | |
| 102 | + } | |
| 103 | + | |
| 104 | + bool | |
| 105 | + has_arg() const | |
| 59 | 106 | { |
| 107 | + return m_parser->has_arg(); | |
| 60 | 108 | } |
| 61 | 109 | |
| 62 | 110 | private: |
| 111 | + boost::any m_value; | |
| 63 | 112 | std::string m_desc; |
| 113 | + std::shared_ptr<const Value> m_parser; | |
| 64 | 114 | }; |
| 65 | 115 | |
| 66 | 116 | class Options |
| ... | ... | @@ -76,7 +126,7 @@ namespace cxxopts |
| 76 | 126 | private: |
| 77 | 127 | friend class OptionAdder; |
| 78 | 128 | |
| 79 | - std::map<char, std::shared_ptr<OptionDetails>> m_short; | |
| 129 | + std::map<std::string, std::shared_ptr<OptionDetails>> m_short; | |
| 80 | 130 | std::map<std::string, std::shared_ptr<OptionDetails>> m_long; |
| 81 | 131 | }; |
| 82 | 132 | |
| ... | ... | @@ -93,7 +143,9 @@ namespace cxxopts |
| 93 | 143 | operator() |
| 94 | 144 | ( |
| 95 | 145 | const std::string& opts, |
| 96 | - const std::string& desc | |
| 146 | + const std::string& desc, | |
| 147 | + std::shared_ptr<const Value> value | |
| 148 | + = std::make_shared<values::Boolean>() | |
| 97 | 149 | ); |
| 98 | 150 | |
| 99 | 151 | private: | ... | ... |