Commit 8a86fbc32f99b37a48c719408970b6dbf900aad9
1 parent
a61dd78a
clean up exceptions a bit
Showing
3 changed files
with
81 additions
and
25 deletions
src/cxxopts.cpp
| ... | ... | @@ -86,33 +86,63 @@ Options::parse(int& argc, char**& argv) |
| 86 | 86 | |
| 87 | 87 | for (int i = 0; i != s.size(); ++i) |
| 88 | 88 | { |
| 89 | - auto iter = m_short.find(std::string(1, s[i])); | |
| 89 | + std::string name(1, s[i]); | |
| 90 | + auto iter = m_short.find(name); | |
| 90 | 91 | |
| 91 | 92 | if (iter == m_short.end()) |
| 92 | 93 | { |
| 94 | + //argument not found | |
| 93 | 95 | } |
| 94 | 96 | |
| 95 | 97 | auto value = iter->second; |
| 96 | 98 | |
| 97 | - if (value->has_arg()) | |
| 99 | + //if no argument then just add it | |
| 100 | + if (!value->has_arg()) | |
| 98 | 101 | { |
| 102 | + auto& v = m_parsed[name]; | |
| 103 | + value->parse("", v); | |
| 104 | + } | |
| 105 | + else | |
| 106 | + { | |
| 107 | + //it must be the last argument | |
| 108 | + if (i + 1 == s.size()) | |
| 109 | + { | |
| 110 | + } | |
| 111 | + else | |
| 112 | + { | |
| 113 | + //error | |
| 114 | + } | |
| 99 | 115 | } |
| 100 | 116 | } |
| 101 | 117 | } |
| 102 | 118 | else if (result[1].length() != 0) |
| 103 | 119 | { |
| 104 | - if (result[3].length() != 0) | |
| 120 | + std::string name = result[1]; | |
| 121 | + | |
| 122 | + auto iter = m_long.find(name); | |
| 123 | + | |
| 124 | + if (iter == m_long.end()) | |
| 105 | 125 | { |
| 106 | - auto iter = m_long.find(result[3]); | |
| 126 | + } | |
| 107 | 127 | |
| 108 | - if (iter == m_long.end()) | |
| 128 | + auto opt = iter->second; | |
| 129 | + | |
| 130 | + //equals provided for long option? | |
| 131 | + if (result[3].length() != 0) | |
| 132 | + { | |
| 133 | + //parse the option given | |
| 134 | + } | |
| 135 | + else | |
| 136 | + { | |
| 137 | + if (opt->has_arg()) | |
| 109 | 138 | { |
| 139 | + //parse the next argument | |
| 140 | + } | |
| 141 | + else | |
| 142 | + { | |
| 143 | + //parse with empty argument | |
| 110 | 144 | } |
| 111 | - | |
| 112 | - auto value = iter->second; | |
| 113 | 145 | } |
| 114 | - | |
| 115 | - //equals provided for long option? | |
| 116 | 146 | } |
| 117 | 147 | |
| 118 | 148 | } | ... | ... |
src/cxxopts.hpp
| ... | ... | @@ -40,19 +40,18 @@ namespace cxxopts |
| 40 | 40 | |
| 41 | 41 | extern std::basic_regex<char> option_specifier; |
| 42 | 42 | |
| 43 | - class OptionException : public std::exception | |
| 44 | - { | |
| 45 | - }; | |
| 43 | + class MessageException | |
| 44 | + ; | |
| 46 | 45 | |
| 47 | - class option_exists_error : public OptionException | |
| 46 | + class OptionException : public std::exception | |
| 48 | 47 | { |
| 49 | 48 | public: |
| 50 | - option_exists_error(const std::string& option) | |
| 49 | + OptionException(const std::string& message) | |
| 50 | + : m_message(message) | |
| 51 | 51 | { |
| 52 | - m_message = u8"Option ‘" + option + u8"’ already exists"; | |
| 53 | 52 | } |
| 54 | 53 | |
| 55 | - const char* | |
| 54 | + virtual const char* | |
| 56 | 55 | what() const noexcept |
| 57 | 56 | { |
| 58 | 57 | return m_message.c_str(); |
| ... | ... | @@ -62,22 +61,41 @@ namespace cxxopts |
| 62 | 61 | std::string m_message; |
| 63 | 62 | }; |
| 64 | 63 | |
| 65 | - class invalid_option_format_error : public OptionException | |
| 64 | + class OptionSpecException : public OptionException | |
| 66 | 65 | { |
| 67 | 66 | public: |
| 68 | - invalid_option_format_error(const std::string& format) | |
| 67 | + | |
| 68 | + OptionSpecException(const std::string& message) | |
| 69 | + : OptionException(message) | |
| 69 | 70 | { |
| 70 | - m_message = u8"Invalid option format ‘" + format + u8"’"; | |
| 71 | 71 | } |
| 72 | + }; | |
| 72 | 73 | |
| 73 | - const char* | |
| 74 | - what() const noexcept | |
| 74 | + class OptionParseException : public OptionException | |
| 75 | + { | |
| 76 | + public: | |
| 77 | + OptionParseException(const std::string& message) | |
| 78 | + : OptionException(message) | |
| 75 | 79 | { |
| 76 | - return m_message.c_str(); | |
| 77 | 80 | } |
| 81 | + }; | |
| 78 | 82 | |
| 79 | - private: | |
| 80 | - std::string m_message; | |
| 83 | + class option_exists_error : public OptionSpecException | |
| 84 | + { | |
| 85 | + public: | |
| 86 | + option_exists_error(const std::string& option) | |
| 87 | + : OptionSpecException(u8"Option ‘" + option + u8"’ already exists") | |
| 88 | + { | |
| 89 | + } | |
| 90 | + }; | |
| 91 | + | |
| 92 | + class invalid_option_format_error : public OptionSpecException | |
| 93 | + { | |
| 94 | + public: | |
| 95 | + invalid_option_format_error(const std::string& format) | |
| 96 | + : OptionSpecException(u8"Invalid option format ‘" + format + u8"’") | |
| 97 | + { | |
| 98 | + } | |
| 81 | 99 | }; |
| 82 | 100 | |
| 83 | 101 | class OptionAdder; |
| ... | ... | @@ -107,8 +125,13 @@ namespace cxxopts |
| 107 | 125 | return m_parser->has_arg(); |
| 108 | 126 | } |
| 109 | 127 | |
| 128 | + void | |
| 129 | + parse(const std::string& text, boost::any& arg) | |
| 130 | + { | |
| 131 | + m_parser->parse(text, arg); | |
| 132 | + } | |
| 133 | + | |
| 110 | 134 | private: |
| 111 | - boost::any m_value; | |
| 112 | 135 | std::string m_desc; |
| 113 | 136 | std::shared_ptr<const Value> m_parser; |
| 114 | 137 | }; |
| ... | ... | @@ -128,6 +151,8 @@ namespace cxxopts |
| 128 | 151 | |
| 129 | 152 | std::map<std::string, std::shared_ptr<OptionDetails>> m_short; |
| 130 | 153 | std::map<std::string, std::shared_ptr<OptionDetails>> m_long; |
| 154 | + | |
| 155 | + std::map<std::string, boost::any> m_parsed; | |
| 131 | 156 | }; |
| 132 | 157 | |
| 133 | 158 | class OptionAdder | ... | ... |