diff options
author | John Wiegley <johnw@newartisans.com> | 2009-02-16 17:30:34 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-02-16 17:30:34 -0400 |
commit | 5dc8f6bccb89bc952d924cf615435c0185168a27 (patch) | |
tree | ca23445faa855ff1fa52171072b7903d4aea3cdc /src | |
parent | b7f2a95c1f630d1246afa4b7f847435d62c9341e (diff) | |
download | fork-ledger-5dc8f6bccb89bc952d924cf615435c0185168a27.tar.gz fork-ledger-5dc8f6bccb89bc952d924cf615435c0185168a27.tar.bz2 fork-ledger-5dc8f6bccb89bc952d924cf615435c0185168a27.zip |
Report better errors if options are missing args
Diffstat (limited to 'src')
-rw-r--r-- | src/option.cc | 8 | ||||
-rw-r--r-- | src/option.h | 20 |
2 files changed, 19 insertions, 9 deletions
diff --git a/src/option.cc b/src/option.cc index 1a68d2a8..f8e6a713 100644 --- a/src/option.cc +++ b/src/option.cc @@ -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, diff --git a/src/option.h b/src/option.h index 7deb5f58..b201cb72 100644 --- a/src/option.h +++ b/src/option.h @@ -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); } |