Fixes #345, fixes #346: Exception code tweaks (#347)

* Fixes #345, fixes #346, regards #340: Put exceptions in a sub-namespace and renamed them accordingly.

* Also fixed some "loose ends" regarding namespace indentation which were missing in PR #350.

* Dropped `required_option_missing` as it is unused.
This commit is contained in:
Eyal Rozenberg 2022-07-12 12:45:58 +03:00 committed by GitHub
parent 548d6196f5
commit 628dc9202b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 336 additions and 337 deletions

View File

@ -109,9 +109,9 @@ result.unmatched()
Exceptional situations throw C++ exceptions. There are two types of
exceptions: errors defining the options, and errors when parsing a list of
arguments. All exceptions derive from `cxxopts::OptionException`. Errors
defining options derive from `cxxopts::OptionSpecException` and errors
parsing arguments derive from `cxxopts::OptionParseException`.
arguments. All exceptions derive from `cxxopts::exceptions::exception`. Errors
defining options derive from `cxxopts::exceptions::specification` and errors
parsing arguments derive from `cxxopts::exceptions::parsing`.
All exceptions define a `what()` function to get a printable string
explaining the error.

View File

@ -227,6 +227,7 @@ empty(const String& s)
} // namespace cxxopts
namespace std {
inline
cxxopts::UnicodeStringIterator
begin(const icu::UnicodeString& s)
@ -304,6 +305,7 @@ empty(const std::string& s)
#endif
namespace cxxopts {
namespace {
#ifdef _WIN32
const std::string LQUOTE("\'");
@ -368,10 +370,11 @@ class Value : public std::enable_shared_from_this<Value>
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
class OptionException : public std::exception
{
namespace exceptions {
class exception : public std::exception
{
public:
explicit OptionException(std::string message)
explicit exception(std::string message)
: m_message(std::move(message))
{
}
@ -385,147 +388,137 @@ class OptionException : public std::exception
private:
std::string m_message;
};
};
class OptionSpecException : public OptionException
{
class specification : public exception
{
public:
explicit OptionSpecException(const std::string& message)
: OptionException(message)
explicit specification(const std::string& message)
: exception(message)
{
}
};
};
class OptionParseException : public OptionException
{
class parsing : public exception
{
public:
explicit OptionParseException(const std::string& message)
: OptionException(message)
explicit parsing(const std::string& message)
: exception(message)
{
}
};
};
class option_exists_error : public OptionSpecException
{
class option_already_exists : public specification
{
public:
explicit option_exists_error(const std::string& option)
: OptionSpecException("Option " + LQUOTE + option + RQUOTE + " already exists")
explicit option_already_exists(const std::string& option)
: specification("Option " + LQUOTE + option + RQUOTE + " already exists")
{
}
};
};
class invalid_option_format_error : public OptionSpecException
{
class invalid_option_format : public specification
{
public:
explicit invalid_option_format_error(const std::string& format)
: OptionSpecException("Invalid option format " + LQUOTE + format + RQUOTE)
explicit invalid_option_format(const std::string& format)
: specification("Invalid option format " + LQUOTE + format + RQUOTE)
{
}
};
};
class option_syntax_exception : public OptionParseException {
class invalid_option_syntax : public parsing {
public:
explicit option_syntax_exception(const std::string& text)
: OptionParseException("Argument " + LQUOTE + text + RQUOTE +
explicit invalid_option_syntax(const std::string& text)
: parsing("Argument " + LQUOTE + text + RQUOTE +
" starts with a - but has incorrect syntax")
{
}
};
};
class option_not_exists_exception : public OptionParseException
{
class no_such_option : public parsing
{
public:
explicit option_not_exists_exception(const std::string& option)
: OptionParseException("Option " + LQUOTE + option + RQUOTE + " does not exist")
explicit no_such_option(const std::string& option)
: parsing("Option " + LQUOTE + option + RQUOTE + " does not exist")
{
}
};
};
class missing_argument_exception : public OptionParseException
{
class missing_argument : public parsing
{
public:
explicit missing_argument_exception(const std::string& option)
: OptionParseException(
explicit missing_argument(const std::string& option)
: parsing(
"Option " + LQUOTE + option + RQUOTE + " is missing an argument"
)
{
}
};
};
class option_requires_argument_exception : public OptionParseException
{
class option_requires_argument : public parsing
{
public:
explicit option_requires_argument_exception(const std::string& option)
: OptionParseException(
explicit option_requires_argument(const std::string& option)
: parsing(
"Option " + LQUOTE + option + RQUOTE + " requires an argument"
)
{
}
};
};
class option_not_has_argument_exception : public OptionParseException
{
class gratuitous_argument_for_option : public parsing
{
public:
option_not_has_argument_exception
gratuitous_argument_for_option
(
const std::string& option,
const std::string& arg
)
: OptionParseException(
: parsing(
"Option " + LQUOTE + option + RQUOTE +
" does not take an argument, but argument " +
LQUOTE + arg + RQUOTE + " given"
)
{
}
};
};
class option_not_present_exception : public OptionParseException
{
class requested_option_not_present : public parsing
{
public:
explicit option_not_present_exception(const std::string& option)
: OptionParseException("Option " + LQUOTE + option + RQUOTE + " not present")
explicit requested_option_not_present(const std::string& option)
: parsing("Option " + LQUOTE + option + RQUOTE + " not present")
{
}
};
};
class option_has_no_value_exception : public OptionException
{
class option_has_no_value : public exception
{
public:
explicit option_has_no_value_exception(const std::string& option)
: OptionException(
explicit option_has_no_value(const std::string& option)
: exception(
!option.empty() ?
("Option " + LQUOTE + option + RQUOTE + " has no value") :
"Option has no value")
{
}
};
};
class argument_incorrect_type : public OptionParseException
{
class incorrect_argument_type : public parsing
{
public:
explicit argument_incorrect_type
explicit incorrect_argument_type
(
const std::string& arg
)
: OptionParseException(
: parsing(
"Argument " + LQUOTE + arg + RQUOTE + " failed to parse"
)
{
}
};
class option_required_exception : public OptionParseException
{
public:
explicit option_required_exception(const std::string& option)
: OptionParseException(
"Option " + LQUOTE + option + RQUOTE + " is required but not present"
)
{
}
};
};
}
template <typename T>
void throw_or_mimic(const std::string& text)
@ -547,7 +540,9 @@ void throw_or_mimic(const std::string& text)
}
namespace values {
namespace parser_tool {
struct IntegerDesc
{
std::string negative = "";
@ -560,12 +555,13 @@ struct ArguDesc {
bool set_value = false;
std::string value = "";
};
#ifdef CXXOPTS_NO_REGEX
inline IntegerDesc SplitInteger(const std::string &text)
{
if (text.empty())
{
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
IntegerDesc desc;
const char *pdata = text.c_str();
@ -585,7 +581,7 @@ inline IntegerDesc SplitInteger(const std::string &text)
}
else
{
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
return desc;
}
@ -644,7 +640,7 @@ inline std::pair<std::string, std::string> SplitSwitchDef(const std::string &tex
if (*pdata == '\0') {
long_sw = std::string(store, pdata - store);
} else {
throw_or_mimic<invalid_option_format_error>(text);
throw_or_mimic<exceptions::invalid_option_format>(text);
}
}
return std::pair<std::string, std::string>(short_sw, long_sw);
@ -725,7 +721,7 @@ inline IntegerDesc SplitInteger(const std::string &text)
if (match.length() == 0)
{
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
IntegerDesc desc;
@ -741,7 +737,6 @@ inline IntegerDesc SplitInteger(const std::string &text)
}
return desc;
}
inline bool IsTrueText(const std::string &text)
@ -764,7 +759,7 @@ inline std::pair<std::string, std::string> SplitSwitchDef(const std::string &tex
std::regex_match(text.c_str(), result, option_specifier);
if (result.empty())
{
throw_or_mimic<invalid_option_format_error>(text);
throw_or_mimic<exceptions::invalid_option_format>(text);
}
const std::string& short_sw = result[2];
@ -799,6 +794,7 @@ inline ArguDesc ParseArgument(const char *arg, bool &matched)
} // namespace parser_tool
namespace detail {
template <typename T, bool B>
struct SignedCheck;
@ -813,14 +809,14 @@ struct SignedCheck<T, true>
{
if (u > static_cast<U>((std::numeric_limits<T>::min)()))
{
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
}
else
{
if (u > static_cast<U>((std::numeric_limits<T>::max)()))
{
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
}
}
@ -857,7 +853,7 @@ template <typename R, typename T>
void
checked_negate(R&, T&&, const std::string& text, std::false_type)
{
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
template <typename T>
@ -893,13 +889,13 @@ integer_parser(const std::string& text, T& value)
}
else
{
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
const US next = static_cast<US>(result * base + digit);
if (result > next)
{
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
result = next;
@ -923,7 +919,7 @@ void stringstream_parser(const std::string& text, T& value)
std::stringstream in(text);
in >> value;
if (!in) {
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
}
@ -951,7 +947,7 @@ parse_value(const std::string& text, bool& value)
return;
}
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
inline
@ -1007,7 +1003,7 @@ void parse_value(const std::string& text, char& c)
{
if (text.length() != 1)
{
throw_or_mimic<argument_incorrect_type>(text);
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
c = text[0];
@ -1204,6 +1200,7 @@ class standard_value<bool> : public abstract_value<bool>
m_implicit_value = "true";
}
};
} // namespace values
template <typename T>
@ -1388,7 +1385,7 @@ class OptionValue
as() const
{
if (m_value == nullptr) {
throw_or_mimic<option_has_no_value_exception>(
throw_or_mimic<exceptions::option_has_no_value>(
m_long_name == nullptr ? "" : *m_long_name);
}
@ -1576,14 +1573,14 @@ class ParseResult
if (iter == m_keys.end())
{
throw_or_mimic<option_not_present_exception>(option);
throw_or_mimic<exceptions::requested_option_not_present>(option);
}
auto viter = m_values.find(iter->second);
if (viter == m_values.end())
{
throw_or_mimic<option_not_present_exception>(option);
throw_or_mimic<exceptions::requested_option_not_present>(option);
}
return viter->second;
@ -1898,15 +1895,16 @@ class OptionAdder
};
namespace {
constexpr size_t OPTION_LONGEST = 30;
constexpr size_t OPTION_DESC_GAP = 2;
String
format_option
(
constexpr size_t OPTION_LONGEST = 30;
constexpr size_t OPTION_DESC_GAP = 2;
String
format_option
(
const HelpOptionDetails& o
)
{
)
{
const auto& s = o.s;
const auto& l = o.l;
@ -1945,17 +1943,17 @@ namespace {
}
return result;
}
}
String
format_description
(
String
format_description
(
const HelpOptionDetails& o,
size_t start,
size_t allowed,
bool tab_expansion
)
{
)
{
auto desc = o.desc;
if (o.has_default && (!o.is_boolean || o.default_value != "false"))
@ -2075,7 +2073,8 @@ namespace {
}
return result;
}
}
} // namespace
inline
@ -2115,11 +2114,11 @@ OptionAdder::operator()
if (!short_sw.length() && !long_sw.length())
{
throw_or_mimic<invalid_option_format_error>(opts);
throw_or_mimic<exceptions::invalid_option_format>(opts);
}
else if (long_sw.length() == 1 && short_sw.length())
{
throw_or_mimic<invalid_option_format_error>(opts);
throw_or_mimic<exceptions::invalid_option_format>(opts);
}
auto option_names = []
@ -2201,7 +2200,7 @@ OptionParser::checked_parse_arg
}
else
{
throw_or_mimic<missing_argument_exception>(name);
throw_or_mimic<exceptions::missing_argument>(name);
}
}
else
@ -2249,7 +2248,7 @@ OptionParser::consume_positional(const std::string& a, PositionalListIterator& n
add_to_option(iter, *next, a);
return true;
}
throw_or_mimic<option_not_exists_exception>(*next);
throw_or_mimic<exceptions::no_such_option>(*next);
}
return false;
@ -2315,7 +2314,7 @@ OptionParser::parse(int argc, const char* const* argv)
// but if it starts with a `-`, then it's an error
if (argv[current][0] == '-' && argv[current][1] != '\0') {
if (!m_allow_unrecognised) {
throw_or_mimic<option_syntax_exception>(argv[current]);
throw_or_mimic<exceptions::invalid_option_syntax>(argv[current]);
}
}
@ -2350,7 +2349,7 @@ OptionParser::parse(int argc, const char* const* argv)
continue;
}
//error
throw_or_mimic<option_not_exists_exception>(name);
throw_or_mimic<exceptions::no_such_option>(name);
}
auto value = iter->second;
@ -2373,7 +2372,7 @@ OptionParser::parse(int argc, const char* const* argv)
else
{
//error
throw_or_mimic<option_requires_argument_exception>(name);
throw_or_mimic<exceptions::option_requires_argument>(name);
}
}
}
@ -2393,7 +2392,7 @@ OptionParser::parse(int argc, const char* const* argv)
continue;
}
//error
throw_or_mimic<option_not_exists_exception>(name);
throw_or_mimic<exceptions::no_such_option>(name);
}
auto opt = iter->second;
@ -2531,7 +2530,7 @@ Options::add_one_option
if (!in.second)
{
throw_or_mimic<option_exists_error>(option);
throw_or_mimic<exceptions::option_already_exists>(option);
}
}

View File

@ -182,7 +182,7 @@ parse(int argc, const char* argv[])
}
std::cout << std::endl;
}
catch (const cxxopts::OptionException& e)
catch (const cxxopts::exceptions::exception& e)
{
std::cout << "error parsing options: " << e.what() << std::endl;
return false;

View File

@ -9188,7 +9188,7 @@ namespace Catch {
// And... Print a result applicable to each result type.
switch( assertionResult.getResultType() ) {
case ResultWas::ThrewException:
m_xml.scopedElement( "Exception" )
m_xml.scopedElement( "exception" )
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
.writeAttribute( "line", assertionResult.getSourceInfo().line )
.writeText( assertionResult.getMessage() );

View File

@ -94,7 +94,7 @@ TEST_CASE("Basic options", "[options]")
CHECK(arguments[2].key() == "value");
CHECK(arguments[3].key() == "av");
CHECK_THROWS_AS(result["nothing"].as<std::string>(), cxxopts::option_has_no_value_exception&);
CHECK_THROWS_AS(result["nothing"].as<std::string>(), cxxopts::exceptions::option_has_no_value&);
CHECK(options.program() == "tester");
}
@ -122,7 +122,7 @@ TEST_CASE("Short options", "[options]")
CHECK(arguments[0].value() == "value");
REQUIRE_THROWS_AS(options.add_options()("", "nothing option"),
cxxopts::invalid_option_format_error&);
cxxopts::exceptions::invalid_option_format&);
}
TEST_CASE("No positional", "[positional]")
@ -235,7 +235,7 @@ TEST_CASE("Positional not valid", "[positional]") {
auto** argv = av.argv();
auto argc = av.argc();
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::no_such_option&);
}
TEST_CASE("Positional with empty arguments", "[positional]") {
@ -294,7 +294,7 @@ TEST_CASE("Boolean without implicit value", "[implicit]")
auto** argv = av.argv();
auto argc = av.argc();
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::missing_argument_exception&);
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::missing_argument&);
}
SECTION("With equal-separated true") {
@ -460,7 +460,7 @@ TEST_CASE("Unsigned integers", "[options]")
auto argc = av.argc();
options.parse_positional("positional");
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::incorrect_argument_type&);
}
TEST_CASE("Integer bounds", "[integer]")
@ -497,12 +497,12 @@ TEST_CASE("Overflow on boundary", "[integer]")
int8_t si;
uint8_t ui;
CHECK_THROWS_AS((integer_parser("128", si)), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS((integer_parser("-129", si)), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS((integer_parser("256", ui)), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS((integer_parser("-0x81", si)), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS((integer_parser("0x80", si)), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS((integer_parser("0x100", ui)), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS((integer_parser("128", si)), cxxopts::exceptions::incorrect_argument_type&);
CHECK_THROWS_AS((integer_parser("-129", si)), cxxopts::exceptions::incorrect_argument_type&);
CHECK_THROWS_AS((integer_parser("256", ui)), cxxopts::exceptions::incorrect_argument_type&);
CHECK_THROWS_AS((integer_parser("-0x81", si)), cxxopts::exceptions::incorrect_argument_type&);
CHECK_THROWS_AS((integer_parser("0x80", si)), cxxopts::exceptions::incorrect_argument_type&);
CHECK_THROWS_AS((integer_parser("0x100", ui)), cxxopts::exceptions::incorrect_argument_type&);
}
TEST_CASE("Integer overflow", "[options]")
@ -519,11 +519,11 @@ TEST_CASE("Integer overflow", "[options]")
auto argc = av.argc();
options.parse_positional("positional");
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::incorrect_argument_type&);
int integer = 0;
CHECK_THROWS_AS((integer_parser("23423423423", integer)), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS((integer_parser("234234234234", integer)), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS((integer_parser("23423423423", integer)), cxxopts::exceptions::incorrect_argument_type&);
CHECK_THROWS_AS((integer_parser("234234234234", integer)), cxxopts::exceptions::incorrect_argument_type&);
}
TEST_CASE("Floats", "[options]")
@ -564,7 +564,7 @@ TEST_CASE("Invalid integers", "[integer]") {
auto argc = av.argc();
options.parse_positional("positional");
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::incorrect_argument_type&);
}
TEST_CASE("Booleans", "[boolean]") {
@ -670,7 +670,7 @@ TEST_CASE("Unrecognised options", "[options]") {
auto argc = av.argc();
SECTION("Default behaviour") {
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::no_such_option&);
}
SECTION("After allowing unrecognised options") {
@ -697,7 +697,7 @@ TEST_CASE("Allow bad short syntax", "[options]") {
auto argc = av.argc();
SECTION("Default behaviour") {
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&);
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::invalid_option_syntax&);
}
SECTION("After allowing unrecognised options") {
@ -720,7 +720,7 @@ TEST_CASE("Invalid option syntax", "[options]") {
auto argc = av.argc();
SECTION("Default behaviour") {
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&);
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::invalid_option_syntax&);
}
}
@ -739,7 +739,7 @@ TEST_CASE("Options empty", "[options]") {
auto** argv = argv_.argv();
CHECK(options.groups().empty());
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::no_such_option&);
}
TEST_CASE("Initializer list with group", "[options]") {