clean up exceptions a bit

This commit is contained in:
Jarryd Beck 2014-09-28 21:09:21 +10:00
parent a61dd78a57
commit 8a86fbc32f
3 changed files with 89 additions and 33 deletions

View File

@ -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
}
}
}
}

View File

@ -40,44 +40,62 @@ namespace cxxopts
extern std::basic_regex<char> 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<const Value> m_parser;
};
@ -128,6 +151,8 @@ namespace cxxopts
std::map<std::string, std::shared_ptr<OptionDetails>> m_short;
std::map<std::string, std::shared_ptr<OptionDetails>> m_long;
std::map<std::string, boost::any> m_parsed;
};
class OptionAdder

View File

@ -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);