From 8a86fbc32f99b37a48c719408970b6dbf900aad9 Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Sun, 28 Sep 2014 21:09:21 +1000 Subject: [PATCH] clean up exceptions a bit --- src/cxxopts.cpp | 50 +++++++++++++++++++++++++++------- src/cxxopts.hpp | 71 +++++++++++++++++++++++++++++++++---------------- src/main.cpp | 1 + 3 files changed, 89 insertions(+), 33 deletions(-) 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 value = iter->second; } + 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 + } + } } } diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index aa89386..f45cf0b 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -40,44 +40,62 @@ namespace cxxopts extern std::basic_regex option_specifier; + class MessageException + ; + class OptionException : public std::exception { + public: + OptionException(const std::string& message) + : m_message(message) + { + } + + virtual const char* + what() const noexcept + { + return m_message.c_str(); + } + + private: + std::string m_message; }; - class option_exists_error : public OptionException + class OptionSpecException : public OptionException + { + public: + + OptionSpecException(const std::string& message) + : OptionException(message) + { + } + }; + + class OptionParseException : public OptionException + { + public: + OptionParseException(const std::string& message) + : OptionException(message) + { + } + }; + + class option_exists_error : public OptionSpecException { public: option_exists_error(const std::string& option) + : OptionSpecException(u8"Option ‘" + option + u8"’ already exists") { - m_message = u8"Option ‘" + option + u8"’ already exists"; } - - const char* - what() const noexcept - { - return m_message.c_str(); - } - - private: - std::string m_message; }; - class invalid_option_format_error : public OptionException + class invalid_option_format_error : public OptionSpecException { public: invalid_option_format_error(const std::string& format) + : OptionSpecException(u8"Invalid option format ‘" + format + u8"’") { - m_message = u8"Invalid option format ‘" + format + u8"’"; } - - const char* - what() const noexcept - { - return m_message.c_str(); - } - - private: - std::string m_message; }; 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);