Commit 97d072ae2dff2ed0c95dc039ab40192413b3f7d0
1 parent
70d458e9
basically working
Showing
3 changed files
with
82 additions
and
66 deletions
src/cxxopts.cpp
| ... | ... | @@ -38,7 +38,7 @@ OptionAdder::operator() |
| 38 | 38 | |
| 39 | 39 | if (s.length() != 0) |
| 40 | 40 | { |
| 41 | - auto in = m_options.m_short.insert(std::make_pair(s.str(), option)); | |
| 41 | + auto in = m_options.m_options.insert(std::make_pair(s.str(), option)); | |
| 42 | 42 | |
| 43 | 43 | if (!in.second) |
| 44 | 44 | { |
| ... | ... | @@ -48,7 +48,7 @@ OptionAdder::operator() |
| 48 | 48 | |
| 49 | 49 | if (l.length() != 0) |
| 50 | 50 | { |
| 51 | - auto in = m_options.m_long.insert(std::make_pair(l, option)); | |
| 51 | + auto in = m_options.m_options.insert(std::make_pair(l, option)); | |
| 52 | 52 | |
| 53 | 53 | if (!in.second) |
| 54 | 54 | { |
| ... | ... | @@ -67,9 +67,7 @@ Options::parse_option |
| 67 | 67 | const std::string& arg |
| 68 | 68 | ) |
| 69 | 69 | { |
| 70 | - auto& v = m_parsed[name]; | |
| 71 | - value->parse("", v.value); | |
| 72 | - ++v.count; | |
| 70 | + value->parse(arg); | |
| 73 | 71 | } |
| 74 | 72 | |
| 75 | 73 | void |
| ... | ... | @@ -121,9 +119,9 @@ Options::parse(int& argc, char**& argv) |
| 121 | 119 | for (int i = 0; i != s.size(); ++i) |
| 122 | 120 | { |
| 123 | 121 | std::string name(1, s[i]); |
| 124 | - auto iter = m_short.find(name); | |
| 122 | + auto iter = m_options.find(name); | |
| 125 | 123 | |
| 126 | - if (iter == m_short.end()) | |
| 124 | + if (iter == m_options.end()) | |
| 127 | 125 | { |
| 128 | 126 | throw option_not_exists_exception(name); |
| 129 | 127 | } |
| ... | ... | @@ -155,9 +153,9 @@ Options::parse(int& argc, char**& argv) |
| 155 | 153 | { |
| 156 | 154 | std::string name = result[1]; |
| 157 | 155 | |
| 158 | - auto iter = m_long.find(name); | |
| 156 | + auto iter = m_options.find(name); | |
| 159 | 157 | |
| 160 | - if (iter == m_long.end()) | |
| 158 | + if (iter == m_options.end()) | |
| 161 | 159 | { |
| 162 | 160 | throw option_not_exists_exception(name); |
| 163 | 161 | } | ... | ... |
src/cxxopts.hpp
| ... | ... | @@ -14,7 +14,7 @@ namespace cxxopts |
| 14 | 14 | public: |
| 15 | 15 | |
| 16 | 16 | virtual void |
| 17 | - parse(const std::string& text, any& result) const = 0; | |
| 17 | + parse(const std::string& text) const = 0; | |
| 18 | 18 | |
| 19 | 19 | virtual bool |
| 20 | 20 | has_arg() const = 0; |
| ... | ... | @@ -23,55 +23,72 @@ namespace cxxopts |
| 23 | 23 | namespace values |
| 24 | 24 | { |
| 25 | 25 | template <typename T> |
| 26 | + void | |
| 27 | + parse_value(const std::string& text, T& value) | |
| 28 | + { | |
| 29 | + std::istringstream is(text); | |
| 30 | + is >> value; | |
| 31 | + } | |
| 32 | + | |
| 33 | + template <typename T> | |
| 34 | + bool | |
| 35 | + value_has_arg(bool*) | |
| 36 | + { | |
| 37 | + return false; | |
| 38 | + } | |
| 39 | + | |
| 40 | + template <typename T> | |
| 41 | + bool | |
| 42 | + value_has_arg(T*) | |
| 43 | + { | |
| 44 | + return true; | |
| 45 | + } | |
| 46 | + | |
| 47 | + template <typename T> | |
| 26 | 48 | class default_value : public Value |
| 27 | 49 | { |
| 28 | - void | |
| 29 | - parse(const std::string& text, any& result) const | |
| 50 | + public: | |
| 51 | + default_value() | |
| 52 | + : m_result(std::make_shared<T>()) | |
| 53 | + , m_store(m_result.get()) | |
| 30 | 54 | { |
| 31 | - T t; | |
| 32 | - std::istringstream is(text); | |
| 33 | - is >> t; | |
| 34 | - result = t; | |
| 35 | 55 | } |
| 36 | 56 | |
| 37 | - bool | |
| 38 | - has_arg() const | |
| 57 | + default_value(T* t) | |
| 58 | + : m_store(t) | |
| 39 | 59 | { |
| 40 | - return true; | |
| 41 | 60 | } |
| 42 | - }; | |
| 43 | 61 | |
| 44 | - template <> | |
| 45 | - class default_value<bool> : public Value | |
| 46 | - { | |
| 47 | 62 | void |
| 48 | - parse(const std::string& text, any& result) const | |
| 63 | + parse(const std::string& text) const | |
| 49 | 64 | { |
| 50 | - result = true; | |
| 65 | + parse_value(text, *m_store); | |
| 51 | 66 | } |
| 52 | 67 | |
| 53 | 68 | bool |
| 54 | 69 | has_arg() const |
| 55 | 70 | { |
| 56 | - return false; | |
| 71 | + return value_has_arg(m_store); | |
| 57 | 72 | } |
| 58 | - }; | |
| 59 | 73 | |
| 60 | - template <> | |
| 61 | - class default_value<std::string> : public Value | |
| 62 | - { | |
| 63 | - void | |
| 64 | - parse(const std::string& text, any& result) const | |
| 74 | + const T& | |
| 75 | + get() const | |
| 65 | 76 | { |
| 66 | - result = text; | |
| 77 | + if (m_store == nullptr) | |
| 78 | + { | |
| 79 | + return *m_result; | |
| 80 | + } | |
| 81 | + else | |
| 82 | + { | |
| 83 | + return *m_store; | |
| 84 | + } | |
| 67 | 85 | } |
| 68 | 86 | |
| 69 | - bool | |
| 70 | - has_arg() const | |
| 71 | - { | |
| 72 | - return true; | |
| 73 | - } | |
| 87 | + private: | |
| 88 | + std::shared_ptr<T> m_result; | |
| 89 | + T* m_store; | |
| 74 | 90 | }; |
| 91 | + | |
| 75 | 92 | } |
| 76 | 93 | |
| 77 | 94 | template <typename T> |
| ... | ... | @@ -205,7 +222,8 @@ namespace cxxopts |
| 205 | 222 | std::shared_ptr<const Value> value |
| 206 | 223 | ) |
| 207 | 224 | : m_desc(description) |
| 208 | - , m_parser(value) | |
| 225 | + , m_value(value) | |
| 226 | + , m_count(0) | |
| 209 | 227 | { |
| 210 | 228 | } |
| 211 | 229 | |
| ... | ... | @@ -218,29 +236,33 @@ namespace cxxopts |
| 218 | 236 | bool |
| 219 | 237 | has_arg() const |
| 220 | 238 | { |
| 221 | - return m_parser->has_arg(); | |
| 239 | + return m_value->has_arg(); | |
| 222 | 240 | } |
| 223 | 241 | |
| 224 | 242 | void |
| 225 | - parse(const std::string& text, boost::any& arg) | |
| 243 | + parse(const std::string& text) | |
| 226 | 244 | { |
| 227 | - m_parser->parse(text, arg); | |
| 245 | + m_value->parse(text); | |
| 246 | + ++m_count; | |
| 228 | 247 | } |
| 229 | 248 | |
| 230 | - private: | |
| 231 | - std::string m_desc; | |
| 232 | - std::shared_ptr<const Value> m_parser; | |
| 233 | - }; | |
| 234 | - | |
| 235 | - struct ParsedOption | |
| 236 | - { | |
| 237 | - boost::any value; | |
| 238 | - int count; | |
| 249 | + int | |
| 250 | + count() const | |
| 251 | + { | |
| 252 | + return m_count; | |
| 253 | + } | |
| 239 | 254 | |
| 240 | - ParsedOption() | |
| 241 | - : count(0) | |
| 255 | + template <typename T> | |
| 256 | + const T& | |
| 257 | + as() const | |
| 242 | 258 | { |
| 259 | + return dynamic_cast<const values::default_value<T>&>(*m_value).get(); | |
| 243 | 260 | } |
| 261 | + | |
| 262 | + private: | |
| 263 | + std::string m_desc; | |
| 264 | + std::shared_ptr<const Value> m_value; | |
| 265 | + int m_count; | |
| 244 | 266 | }; |
| 245 | 267 | |
| 246 | 268 | class Options |
| ... | ... | @@ -256,27 +278,26 @@ namespace cxxopts |
| 256 | 278 | int |
| 257 | 279 | count(const std::string& o) const |
| 258 | 280 | { |
| 259 | - auto iter = m_parsed.find(o); | |
| 260 | - | |
| 261 | - if (iter == m_parsed.end()) | |
| 281 | + auto iter = m_options.find(o); | |
| 282 | + if (iter == m_options.end()) | |
| 262 | 283 | { |
| 263 | 284 | return 0; |
| 264 | 285 | } |
| 265 | 286 | |
| 266 | - return iter->second.count; | |
| 287 | + return iter->second->count(); | |
| 267 | 288 | } |
| 268 | 289 | |
| 269 | - const boost::any& | |
| 290 | + const OptionDetails& | |
| 270 | 291 | operator[](const std::string& option) const |
| 271 | 292 | { |
| 272 | - auto iter = m_parsed.find(option); | |
| 293 | + auto iter = m_options.find(option); | |
| 273 | 294 | |
| 274 | - if (iter == m_parsed.end()) | |
| 295 | + if (iter == m_options.end()) | |
| 275 | 296 | { |
| 276 | 297 | throw option_not_present_exception(option); |
| 277 | 298 | } |
| 278 | 299 | |
| 279 | - return iter->second.value; | |
| 300 | + return *iter->second; | |
| 280 | 301 | } |
| 281 | 302 | |
| 282 | 303 | private: |
| ... | ... | @@ -301,10 +322,7 @@ namespace cxxopts |
| 301 | 322 | ); |
| 302 | 323 | |
| 303 | 324 | |
| 304 | - std::map<std::string, std::shared_ptr<OptionDetails>> m_short; | |
| 305 | - std::map<std::string, std::shared_ptr<OptionDetails>> m_long; | |
| 306 | - | |
| 307 | - std::map<std::string, ParsedOption> m_parsed; | |
| 325 | + std::map<std::string, std::shared_ptr<OptionDetails>> m_options; | |
| 308 | 326 | }; |
| 309 | 327 | |
| 310 | 328 | class OptionAdder | ... | ... |
src/main.cpp