diff --git a/src/cxxopts.cpp b/src/cxxopts.cpp index 8e502fa..fb43c25 100644 --- a/src/cxxopts.cpp +++ b/src/cxxopts.cpp @@ -86,33 +86,63 @@ Options::parse(int& argc, char**& argv) for (int i = 0; i != s.size(); ++i) { - auto iter = m_short.find(std::string(1, s[i])); + std::string name(1, s[i]); + auto iter = m_short.find(name); if (iter == m_short.end()) { + //argument not found } auto value = iter->second; - if (value->has_arg()) + //if no argument then just add it + if (!value->has_arg()) { + auto& v = m_parsed[name]; + value->parse("", v); + } + else + { + //it must be the last argument + if (i + 1 == s.size()) + { + } + else + { + //error + } } } } else if (result[1].length() != 0) { - if (result[3].length() != 0) + std::string name = result[1]; + + auto iter = m_long.find(name); + + if (iter == m_long.end()) { - auto iter = m_long.find(result[3]); + } - if (iter == m_long.end()) + auto opt = iter->second; + + //equals provided for long option? + if (result[3].length() != 0) + { + //parse the option given + } + else + { + if (opt->has_arg()) { + //parse the next argument + } + else + { + //parse with empty argument } - - auto value = iter->second; } - - //equals provided for long option? } } diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index aa89386..f45cf0b 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -40,19 +40,18 @@ namespace cxxopts extern std::basic_regex option_specifier; - class OptionException : public std::exception - { - }; + class MessageException + ; - class option_exists_error : public OptionException + class OptionException : public std::exception { public: - option_exists_error(const std::string& option) + OptionException(const std::string& message) + : m_message(message) { - m_message = u8"Option ‘" + option + u8"’ already exists"; } - const char* + virtual const char* what() const noexcept { return m_message.c_str(); @@ -62,22 +61,41 @@ namespace cxxopts std::string m_message; }; - class invalid_option_format_error : public OptionException + class OptionSpecException : public OptionException { public: - invalid_option_format_error(const std::string& format) + + OptionSpecException(const std::string& message) + : OptionException(message) { - m_message = u8"Invalid option format ‘" + format + u8"’"; } + }; - const char* - what() const noexcept + class OptionParseException : public OptionException + { + public: + OptionParseException(const std::string& message) + : OptionException(message) { - return m_message.c_str(); } + }; - private: - std::string m_message; + class option_exists_error : public OptionSpecException + { + public: + option_exists_error(const std::string& option) + : OptionSpecException(u8"Option ‘" + option + u8"’ already exists") + { + } + }; + + class invalid_option_format_error : public OptionSpecException + { + public: + invalid_option_format_error(const std::string& format) + : OptionSpecException(u8"Invalid option format ‘" + format + u8"’") + { + } }; class OptionAdder; @@ -107,8 +125,13 @@ namespace cxxopts return m_parser->has_arg(); } + void + parse(const std::string& text, boost::any& arg) + { + m_parser->parse(text, arg); + } + private: - boost::any m_value; std::string m_desc; std::shared_ptr m_parser; }; @@ -128,6 +151,8 @@ namespace cxxopts std::map> m_short; std::map> m_long; + + std::map m_parsed; }; class OptionAdder diff --git a/src/main.cpp b/src/main.cpp index 1f58c9f..df6a4cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,7 @@ int main(int argc, char* argv[]) options.add_options() ("a,apple", "an apple") ("b,bob", "Bob") + ("b,bob", "Bob") ; options.parse(argc, argv);