Report better errors if options are missing args

This commit is contained in:
John Wiegley 2009-02-16 17:30:34 -04:00
parent b7f2a95c1f
commit 5dc8f6bccb
2 changed files with 19 additions and 9 deletions

View file

@ -194,8 +194,8 @@ strings_list process_arguments(strings_list args, scope_t& scope)
if (! opt.first)
throw_(option_error, "illegal option --" << name);
if (opt.second && value == NULL) {
value = (*++i).c_str();
if (opt.second && ++i != args.end() && value == NULL) {
value = (*i).c_str();
DEBUG("option.args", " read option value from arg: " << value);
if (value == NULL)
throw_(option_error, "missing option argument for --" << name);
@ -222,8 +222,8 @@ strings_list process_arguments(strings_list args, scope_t& scope)
foreach (op_bool_char_tuple& o, option_queue) {
const char * value = NULL;
if (o.truth) {
value = (*++i).c_str();
if (o.truth && ++i != args.end()) {
value = (*i).c_str();
DEBUG("option.args", " read option value from arg: " << value);
if (value == NULL)
throw_(option_error,

View file

@ -90,10 +90,17 @@ public:
string desc() const {
std::ostringstream out;
out << "--";
for (const char * p = name; *p; p++) {
if (*p == '_') {
if (*(p + 1))
out << '-';
} else {
out << *p;
}
}
if (ch)
out << "--" << name << " (-" << ch << ")";
else
out << "--" << name;
out << " (-" << ch << ")";
return out.str();
}
@ -136,10 +143,13 @@ public:
virtual void handler_thunk(call_scope_t&) {}
virtual void handler(call_scope_t& args) {
if (wants_arg)
if (wants_arg) {
if (args.empty())
throw_(std::runtime_error, "No argument provided for " << desc());
on(args[0]);
else
} else {
on();
}
handler_thunk(args);
}