clean up exceptions a bit
This commit is contained in:
parent
a61dd78a57
commit
8a86fbc32f
@ -86,33 +86,63 @@ Options::parse(int& argc, char**& argv)
|
|||||||
|
|
||||||
for (int i = 0; i != s.size(); ++i)
|
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())
|
if (iter == m_short.end())
|
||||||
{
|
{
|
||||||
|
//argument not found
|
||||||
}
|
}
|
||||||
|
|
||||||
auto value = iter->second;
|
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)
|
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?
|
//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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,44 +40,62 @@ namespace cxxopts
|
|||||||
|
|
||||||
extern std::basic_regex<char> option_specifier;
|
extern std::basic_regex<char> option_specifier;
|
||||||
|
|
||||||
|
class MessageException
|
||||||
|
;
|
||||||
|
|
||||||
class OptionException : public std::exception
|
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:
|
public:
|
||||||
option_exists_error(const std::string& option)
|
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:
|
public:
|
||||||
invalid_option_format_error(const std::string& format)
|
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;
|
class OptionAdder;
|
||||||
@ -107,8 +125,13 @@ namespace cxxopts
|
|||||||
return m_parser->has_arg();
|
return m_parser->has_arg();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
parse(const std::string& text, boost::any& arg)
|
||||||
|
{
|
||||||
|
m_parser->parse(text, arg);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
boost::any m_value;
|
|
||||||
std::string m_desc;
|
std::string m_desc;
|
||||||
std::shared_ptr<const Value> m_parser;
|
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_short;
|
||||||
std::map<std::string, std::shared_ptr<OptionDetails>> m_long;
|
std::map<std::string, std::shared_ptr<OptionDetails>> m_long;
|
||||||
|
|
||||||
|
std::map<std::string, boost::any> m_parsed;
|
||||||
};
|
};
|
||||||
|
|
||||||
class OptionAdder
|
class OptionAdder
|
||||||
|
|||||||
@ -31,6 +31,7 @@ int main(int argc, char* argv[])
|
|||||||
options.add_options()
|
options.add_options()
|
||||||
("a,apple", "an apple")
|
("a,apple", "an apple")
|
||||||
("b,bob", "Bob")
|
("b,bob", "Bob")
|
||||||
|
("b,bob", "Bob")
|
||||||
;
|
;
|
||||||
|
|
||||||
options.parse(argc, argv);
|
options.parse(argc, argv);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user