fix(command_line): Use specific exceptions
This commit is contained in:
parent
7efb2fc47a
commit
2921239833
1 changed files with 10 additions and 8 deletions
|
@ -10,6 +10,8 @@
|
||||||
LEMONBUDDY_NS
|
LEMONBUDDY_NS
|
||||||
|
|
||||||
namespace command_line {
|
namespace command_line {
|
||||||
|
DEFINE_ERROR(argument_error);
|
||||||
|
DEFINE_ERROR(value_error);
|
||||||
|
|
||||||
class option;
|
class option;
|
||||||
using choices = vector<string>;
|
using choices = vector<string>;
|
||||||
|
@ -134,9 +136,9 @@ namespace command_line {
|
||||||
template <class T = parser>
|
template <class T = parser>
|
||||||
static di::injector<T> configure(string scriptname, options opts) {
|
static di::injector<T> configure(string scriptname, options opts) {
|
||||||
// clang-format off
|
// clang-format off
|
||||||
return di::make_injector(
|
return di::make_injector(
|
||||||
di::bind<>().to("Usage: " + scriptname + " bar_name [OPTION...]"),
|
di::bind<>().to("Usage: " + scriptname + " bar_name [OPTION...]"),
|
||||||
di::bind<>().to(opts));
|
di::bind<>().to(opts));
|
||||||
// clang-format on
|
// clang-format on
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,18 +173,18 @@ namespace command_line {
|
||||||
string value;
|
string value;
|
||||||
|
|
||||||
if (input_next.empty() && opt.compare(0, 2, "--") != 0)
|
if (input_next.empty() && opt.compare(0, 2, "--") != 0)
|
||||||
throw application_error("Missing value for " + opt);
|
throw value_error("Missing value for " + opt);
|
||||||
else if (!input_next.empty())
|
else if (!input_next.empty())
|
||||||
value = input_next;
|
value = input_next;
|
||||||
else if ((pos = opt.compare(0, 1, "=")) == string::npos)
|
else if ((pos = opt.find("=")) == string::npos)
|
||||||
throw application_error("Missing value for " + opt);
|
throw value_error("Missing value for " + opt);
|
||||||
else {
|
else {
|
||||||
value = opt.substr(pos + 1);
|
value = opt.substr(pos + 1);
|
||||||
opt = opt.substr(0, pos);
|
opt = opt.substr(0, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!values.empty() && std::find(values.begin(), values.end(), value) == values.end())
|
if (!values.empty() && std::find(values.begin(), values.end(), value) == values.end())
|
||||||
throw application_error("Invalid argument '" + value + "' for " + string{opt});
|
throw value_error("Invalid value '" + value + "' for argument " + string{opt});
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
@ -212,7 +214,7 @@ namespace command_line {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input.compare(0, 1, "-") == 0) {
|
if (input.compare(0, 1, "-") == 0) {
|
||||||
throw application_error("Unrecognized option " + input);
|
throw argument_error("Unrecognized option " + input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue