Commit 6f51b4f218e013091248d260554390622ac4ce55
1 parent
b89dc24e
ready to test
Showing
2 changed files
with
90 additions
and
4 deletions
src/cxxopts.cpp
| @@ -60,6 +60,37 @@ OptionAdder::operator() | @@ -60,6 +60,37 @@ OptionAdder::operator() | ||
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | void | 62 | void |
| 63 | +Options::parse_option | ||
| 64 | +( | ||
| 65 | + std::shared_ptr<OptionDetails> value, | ||
| 66 | + const std::string& name, | ||
| 67 | + const std::string& arg | ||
| 68 | +) | ||
| 69 | +{ | ||
| 70 | + auto& v = m_parsed[name]; | ||
| 71 | + value->parse("", v.value); | ||
| 72 | + ++v.count; | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +void | ||
| 76 | +Options::checked_parse_arg | ||
| 77 | +( | ||
| 78 | + int argc, | ||
| 79 | + char* argv[], | ||
| 80 | + int argPos, | ||
| 81 | + std::shared_ptr<OptionDetails> value, | ||
| 82 | + const std::string& name | ||
| 83 | +) | ||
| 84 | +{ | ||
| 85 | + if (argPos >= argc) | ||
| 86 | + { | ||
| 87 | + throw missing_argument_exception(name); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + parse_option(value, name, argv[argPos]); | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +void | ||
| 63 | Options::parse(int& argc, char**& argv) | 94 | Options::parse(int& argc, char**& argv) |
| 64 | { | 95 | { |
| 65 | int current = 1; | 96 | int current = 1; |
| @@ -75,6 +106,9 @@ Options::parse(int& argc, char**& argv) | @@ -75,6 +106,9 @@ Options::parse(int& argc, char**& argv) | ||
| 75 | { | 106 | { |
| 76 | //handle empty | 107 | //handle empty |
| 77 | 108 | ||
| 109 | + //for now, throw an exception | ||
| 110 | + throw option_not_exists_exception(argv[current]); | ||
| 111 | + | ||
| 78 | //if we return from here then it was parsed successfully, so continue | 112 | //if we return from here then it was parsed successfully, so continue |
| 79 | } | 113 | } |
| 80 | else | 114 | else |
| @@ -92,7 +126,6 @@ Options::parse(int& argc, char**& argv) | @@ -92,7 +126,6 @@ Options::parse(int& argc, char**& argv) | ||
| 92 | if (iter == m_short.end()) | 126 | if (iter == m_short.end()) |
| 93 | { | 127 | { |
| 94 | throw option_not_exists_exception(name); | 128 | throw option_not_exists_exception(name); |
| 95 | - //argument not found | ||
| 96 | } | 129 | } |
| 97 | 130 | ||
| 98 | auto value = iter->second; | 131 | auto value = iter->second; |
| @@ -100,15 +133,14 @@ Options::parse(int& argc, char**& argv) | @@ -100,15 +133,14 @@ Options::parse(int& argc, char**& argv) | ||
| 100 | //if no argument then just add it | 133 | //if no argument then just add it |
| 101 | if (!value->has_arg()) | 134 | if (!value->has_arg()) |
| 102 | { | 135 | { |
| 103 | - auto& v = m_parsed[name]; | ||
| 104 | - value->parse("", v.value); | ||
| 105 | - ++v.count; | 136 | + parse_option(value, name); |
| 106 | } | 137 | } |
| 107 | else | 138 | else |
| 108 | { | 139 | { |
| 109 | //it must be the last argument | 140 | //it must be the last argument |
| 110 | if (i + 1 == s.size()) | 141 | if (i + 1 == s.size()) |
| 111 | { | 142 | { |
| 143 | + checked_parse_arg(argc, argv, current+1, value, name); | ||
| 112 | } | 144 | } |
| 113 | else | 145 | else |
| 114 | { | 146 | { |
| @@ -135,16 +167,26 @@ Options::parse(int& argc, char**& argv) | @@ -135,16 +167,26 @@ Options::parse(int& argc, char**& argv) | ||
| 135 | if (result[3].length() != 0) | 167 | if (result[3].length() != 0) |
| 136 | { | 168 | { |
| 137 | //parse the option given | 169 | //parse the option given |
| 170 | + | ||
| 171 | + //but if it doesn't take an argument, this is an error | ||
| 172 | + if (!opt->has_arg()) | ||
| 173 | + { | ||
| 174 | + throw option_not_has_argument_exception(name, result[3]); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + parse_option(opt, name, result[3]); | ||
| 138 | } | 178 | } |
| 139 | else | 179 | else |
| 140 | { | 180 | { |
| 141 | if (opt->has_arg()) | 181 | if (opt->has_arg()) |
| 142 | { | 182 | { |
| 143 | //parse the next argument | 183 | //parse the next argument |
| 184 | + checked_parse_arg(argc, argv, current + 1, opt, name); | ||
| 144 | } | 185 | } |
| 145 | else | 186 | else |
| 146 | { | 187 | { |
| 147 | //parse with empty argument | 188 | //parse with empty argument |
| 189 | + parse_option(opt, name); | ||
| 148 | } | 190 | } |
| 149 | } | 191 | } |
| 150 | } | 192 | } |
src/cxxopts.hpp
| @@ -26,6 +26,7 @@ namespace cxxopts | @@ -26,6 +26,7 @@ namespace cxxopts | ||
| 26 | void | 26 | void |
| 27 | parse(const std::string& text, any& result) const | 27 | parse(const std::string& text, any& result) const |
| 28 | { | 28 | { |
| 29 | + result = true; | ||
| 29 | } | 30 | } |
| 30 | 31 | ||
| 31 | bool | 32 | bool |
| @@ -107,6 +108,15 @@ namespace cxxopts | @@ -107,6 +108,15 @@ namespace cxxopts | ||
| 107 | } | 108 | } |
| 108 | }; | 109 | }; |
| 109 | 110 | ||
| 111 | + class missing_argument_exception : public OptionParseException | ||
| 112 | + { | ||
| 113 | + public: | ||
| 114 | + missing_argument_exception(const std::string& option) | ||
| 115 | + : OptionParseException(u8"Option ‘" + option + u8"’ is missing an argument") | ||
| 116 | + { | ||
| 117 | + } | ||
| 118 | + }; | ||
| 119 | + | ||
| 110 | class option_requires_argument_exception : public OptionParseException | 120 | class option_requires_argument_exception : public OptionParseException |
| 111 | { | 121 | { |
| 112 | public: | 122 | public: |
| @@ -116,6 +126,21 @@ namespace cxxopts | @@ -116,6 +126,21 @@ namespace cxxopts | ||
| 116 | } | 126 | } |
| 117 | }; | 127 | }; |
| 118 | 128 | ||
| 129 | + class option_not_has_argument_exception : public OptionParseException | ||
| 130 | + { | ||
| 131 | + public: | ||
| 132 | + option_not_has_argument_exception | ||
| 133 | + ( | ||
| 134 | + const std::string& option, | ||
| 135 | + const std::string& arg | ||
| 136 | + ) | ||
| 137 | + : OptionParseException( | ||
| 138 | + u8"Option ‘" + option + u8"’ does not take an argument, but argument‘" | ||
| 139 | + + arg + "’ given") | ||
| 140 | + { | ||
| 141 | + } | ||
| 142 | + }; | ||
| 143 | + | ||
| 119 | class option_not_present_exception : public OptionParseException | 144 | class option_not_present_exception : public OptionParseException |
| 120 | { | 145 | { |
| 121 | public: | 146 | public: |
| @@ -213,6 +238,25 @@ namespace cxxopts | @@ -213,6 +238,25 @@ namespace cxxopts | ||
| 213 | private: | 238 | private: |
| 214 | friend class OptionAdder; | 239 | friend class OptionAdder; |
| 215 | 240 | ||
| 241 | + void | ||
| 242 | + parse_option | ||
| 243 | + ( | ||
| 244 | + std::shared_ptr<OptionDetails> value, | ||
| 245 | + const std::string& name, | ||
| 246 | + const std::string& arg = "" | ||
| 247 | + ); | ||
| 248 | + | ||
| 249 | + void | ||
| 250 | + checked_parse_arg | ||
| 251 | + ( | ||
| 252 | + int argc, | ||
| 253 | + char* argv[], | ||
| 254 | + int argPos, | ||
| 255 | + std::shared_ptr<OptionDetails> value, | ||
| 256 | + const std::string& name | ||
| 257 | + ); | ||
| 258 | + | ||
| 259 | + | ||
| 216 | std::map<std::string, std::shared_ptr<OptionDetails>> m_short; | 260 | std::map<std::string, std::shared_ptr<OptionDetails>> m_short; |
| 217 | std::map<std::string, std::shared_ptr<OptionDetails>> m_long; | 261 | std::map<std::string, std::shared_ptr<OptionDetails>> m_long; |
| 218 | 262 |