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) if (! opt.first)
throw_(option_error, "illegal option --" << name); throw_(option_error, "illegal option --" << name);
if (opt.second && value == NULL) { if (opt.second && ++i != args.end() && value == NULL) {
value = (*++i).c_str(); value = (*i).c_str();
DEBUG("option.args", " read option value from arg: " << value); DEBUG("option.args", " read option value from arg: " << value);
if (value == NULL) if (value == NULL)
throw_(option_error, "missing option argument for --" << name); 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) { foreach (op_bool_char_tuple& o, option_queue) {
const char * value = NULL; const char * value = NULL;
if (o.truth) { if (o.truth && ++i != args.end()) {
value = (*++i).c_str(); value = (*i).c_str();
DEBUG("option.args", " read option value from arg: " << value); DEBUG("option.args", " read option value from arg: " << value);
if (value == NULL) if (value == NULL)
throw_(option_error, throw_(option_error,

View file

@ -90,10 +90,17 @@ public:
string desc() const { string desc() const {
std::ostringstream out; std::ostringstream out;
out << "--";
for (const char * p = name; *p; p++) {
if (*p == '_') {
if (*(p + 1))
out << '-';
} else {
out << *p;
}
}
if (ch) if (ch)
out << "--" << name << " (-" << ch << ")"; out << " (-" << ch << ")";
else
out << "--" << name;
return out.str(); return out.str();
} }
@ -136,10 +143,13 @@ public:
virtual void handler_thunk(call_scope_t&) {} virtual void handler_thunk(call_scope_t&) {}
virtual void handler(call_scope_t& args) { 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]); on(args[0]);
else } else {
on(); on();
}
handler_thunk(args); handler_thunk(args);
} }