Fix: Make CXXOPTS_NO_EXCEPTIONS compilable and print messages (#196)

Fix CXXOPTS_NO_EXCEPTIONS so that it doesn't warn about unused parameters, and print out error messages.
This commit is contained in:
charlydelta 2019-08-08 00:25:34 +02:00 committed by jarro2783
parent fce82fb035
commit 34784b01f7

View File

@ -467,6 +467,26 @@ namespace cxxopts
} }
}; };
template <typename T>
void throw_or_mimic(const std::string& text)
{
static_assert(std::is_base_of<std::exception, T>::value,
"throw_or_mimic only works on std::exception and "
"deriving classes");
#ifndef CXXOPTS_NO_EXCEPTIONS
// If CXXOPTS_NO_EXCEPTIONS is not defined, just throw
throw T{text};
#else
// Otherwise manually instantiate the exception, print what() to stderr,
// and abort
T exception{text};
std::cerr << exception.what() << std::endl;
std::cerr << "Aborting (exceptions disabled)..." << std::endl;
std::abort();
#endif
}
namespace values namespace values
{ {
namespace namespace
@ -495,22 +515,14 @@ namespace cxxopts
{ {
if (u > static_cast<U>((std::numeric_limits<T>::min)())) if (u > static_cast<U>((std::numeric_limits<T>::min)()))
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<argument_incorrect_type>(text);
throw argument_incorrect_type(text);
#else
std::abort();
#endif
} }
} }
else else
{ {
if (u > static_cast<U>((std::numeric_limits<T>::max)())) if (u > static_cast<U>((std::numeric_limits<T>::max)()))
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<argument_incorrect_type>(text);
throw argument_incorrect_type(text);
#else
std::abort();
#endif
} }
} }
} }
@ -544,13 +556,10 @@ namespace cxxopts
template <typename R, typename T> template <typename R, typename T>
T T
checked_negate(T&&, const std::string& text, std::false_type) checked_negate(T&& t, const std::string& text, std::false_type)
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<argument_incorrect_type>(text);
throw argument_incorrect_type(text); return t;
#else
std::abort();
#endif
} }
template <typename T> template <typename T>
@ -562,11 +571,7 @@ namespace cxxopts
if (match.length() == 0) if (match.length() == 0)
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<argument_incorrect_type>(text);
throw argument_incorrect_type(text);
#else
std::abort();
#endif
} }
if (match.length(4) > 0) if (match.length(4) > 0)
@ -603,21 +608,13 @@ namespace cxxopts
} }
else else
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<argument_incorrect_type>(text);
throw argument_incorrect_type(text);
#else
std::abort();
#endif
} }
US next = result * base + digit; US next = result * base + digit;
if (result > next) if (result > next)
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<argument_incorrect_type>(text);
throw argument_incorrect_type(text);
#else
std::abort();
#endif
} }
result = next; result = next;
@ -643,11 +640,7 @@ namespace cxxopts
std::stringstream in(text); std::stringstream in(text);
in >> value; in >> value;
if (!in) { if (!in) {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<argument_incorrect_type>(text);
throw argument_incorrect_type(text);
#else
std::abort();
#endif
} }
} }
@ -727,11 +720,7 @@ namespace cxxopts
return; return;
} }
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<argument_incorrect_type>(text);
throw argument_incorrect_type(text);
#else
std::abort();
#endif
} }
inline inline
@ -1108,11 +1097,7 @@ namespace cxxopts
as() const as() const
{ {
if (m_value == nullptr) { if (m_value == nullptr) {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<std::domain_error>("No value");
throw std::domain_error("No value");
#else
std::abort();
#endif
} }
#ifdef CXXOPTS_NO_RTTI #ifdef CXXOPTS_NO_RTTI
@ -1207,11 +1192,7 @@ namespace cxxopts
if (iter == m_options->end()) if (iter == m_options->end())
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<option_not_present_exception>(option);
throw option_not_present_exception(option);
#else
std::abort();
#endif
} }
auto riter = m_results.find(iter->second); auto riter = m_results.find(iter->second);
@ -1269,8 +1250,8 @@ namespace cxxopts
std::vector<KeyValue> m_sequential; std::vector<KeyValue> m_sequential;
}; };
struct Option struct Option
{ {
Option Option
( (
@ -1290,7 +1271,7 @@ namespace cxxopts
std::string desc_; std::string desc_;
std::shared_ptr<const Value> value_; std::shared_ptr<const Value> value_;
std::string arg_help_; std::string arg_help_;
}; };
class Options class Options
{ {
@ -1343,20 +1324,20 @@ namespace cxxopts
OptionAdder OptionAdder
add_options(std::string group = ""); add_options(std::string group = "");
void
add_options
(
const std::string& group,
std::initializer_list<Option> options
);
void void
add_option add_options
( (
const std::string& group, const std::string& group,
const Option& option std::initializer_list<Option> options
); );
void
add_option
(
const std::string& group,
const Option& option
);
void void
add_option add_option
@ -1597,15 +1578,15 @@ ParseResult::ParseResult
} }
inline inline
void void
Options::add_options Options::add_options
( (
const std::string &group, const std::string &group,
std::initializer_list<Option> options std::initializer_list<Option> options
) )
{ {
OptionAdder option_adder(*this, group); OptionAdder option_adder(*this, group);
for (const auto &option: options) for (const auto &option: options)
{ {
option_adder(option.opts_, option.desc_, option.value_, option.arg_help_); option_adder(option.opts_, option.desc_, option.value_, option.arg_help_);
} }
@ -1633,11 +1614,7 @@ OptionAdder::operator()
if (result.empty()) if (result.empty())
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<invalid_option_format_error>(opts);
throw invalid_option_format_error(opts);
#else
std::abort();
#endif
} }
const auto& short_match = result[2]; const auto& short_match = result[2];
@ -1645,18 +1622,10 @@ OptionAdder::operator()
if (!short_match.length() && !long_match.length()) if (!short_match.length() && !long_match.length())
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<invalid_option_format_error>(opts);
throw invalid_option_format_error(opts);
#else
std::abort();
#endif
} else if (long_match.length() == 1 && short_match.length()) } else if (long_match.length() == 1 && short_match.length())
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<invalid_option_format_error>(opts);
throw invalid_option_format_error(opts);
#else
std::abort();
#endif
} }
auto option_names = [] auto option_names = []
@ -1729,11 +1698,7 @@ ParseResult::checked_parse_arg
} }
else else
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<missing_argument_exception>(name);
throw missing_argument_exception(name);
#else
std::abort();
#endif
} }
} }
else else
@ -1758,11 +1723,7 @@ ParseResult::add_to_option(const std::string& option, const std::string& arg)
if (iter == m_options->end()) if (iter == m_options->end())
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<option_not_exists_exception>(option);
throw option_not_exists_exception(option);
#else
std::abort();
#endif
} }
parse_option(iter->second, option, arg); parse_option(iter->second, option, arg);
@ -1800,11 +1761,7 @@ ParseResult::consume_positional(std::string a)
} }
else else
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<option_not_exists_exception>(*m_next_positional);
throw option_not_exists_exception(*m_next_positional);
#else
std::abort();
#endif
} }
} }
@ -1872,11 +1829,7 @@ ParseResult::parse(int& argc, char**& argv)
// but if it starts with a `-`, then it's an error // but if it starts with a `-`, then it's an error
if (argv[current][0] == '-' && argv[current][1] != '\0') { if (argv[current][0] == '-' && argv[current][1] != '\0') {
if (!m_allow_unrecognised) { if (!m_allow_unrecognised) {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<option_syntax_exception>(argv[current]);
throw option_syntax_exception(argv[current]);
#else
std::abort();
#endif
} }
} }
@ -1913,11 +1866,7 @@ ParseResult::parse(int& argc, char**& argv)
else else
{ {
//error //error
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<option_not_exists_exception>(name);
throw option_not_exists_exception(name);
#else
std::abort();
#endif
} }
} }
@ -1935,11 +1884,7 @@ ParseResult::parse(int& argc, char**& argv)
else else
{ {
//error //error
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<option_requires_argument_exception>(name);
throw option_requires_argument_exception(name);
#else
std::abort();
#endif
} }
} }
} }
@ -1962,11 +1907,7 @@ ParseResult::parse(int& argc, char**& argv)
else else
{ {
//error //error
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<option_not_exists_exception>(name);
throw option_not_exists_exception(name);
#else
std::abort();
#endif
} }
} }
@ -2033,7 +1974,7 @@ Options::add_option
const Option& option const Option& option
) )
{ {
add_options(group, {option}); add_options(group, {option});
} }
inline inline
@ -2084,11 +2025,7 @@ Options::add_one_option
if (!in.second) if (!in.second)
{ {
#ifndef CXXOPTS_NO_EXCEPTIONS throw_or_mimic<option_exists_error>(option);
throw option_exists_error(option);
#else
std::abort();
#endif
} }
} }