From 0b9f22b4d24e8fa545af2d7d448ddfe9fb3736ba Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 22 Jan 2009 16:27:24 -0400 Subject: Redid the way command-line arguments are processed. Before, Ledger used - and -- to mean special things after the command verb was seen. But now, what used to be specified as this: ledger -n reg cash -payable -- shell Is now specified as this: ledger reg -n cash not payable @shell It could also be specified as: ledger -n reg \(cash and not payable\) and @shell --- src/option.cc | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'src/option.cc') diff --git a/src/option.cc b/src/option.cc index 2e74f3b0..fa7e659d 100644 --- a/src/option.cc +++ b/src/option.cc @@ -139,31 +139,36 @@ void process_environment(const char ** envp, const string& tag, } } -void process_arguments(int, char ** argv, const bool anywhere, - scope_t& scope, std::list& args) +void process_arguments(int, char ** argv, scope_t& scope, + std::list& args) { + bool anywhere = true; + for (char ** i = argv; *i; i++) { - if ((*i)[0] != '-') { - if (anywhere) { - args.push_back(*i); - continue; - } else { - for (; *i; i++) - args.push_back(*i); - break; - } + DEBUG("option.args", "Examining argument '" << *i << "'"); + + if (! anywhere || (*i)[0] != '-') { + DEBUG("option.args", " adding to list of real args"); + args.push_back(*i); + continue; } // --long-option or -s if ((*i)[1] == '-') { - if ((*i)[2] == '\0') - break; + if ((*i)[2] == '\0') { + DEBUG("option.args", " it's a --, ending options processing"); + anywhere = false; + continue; + } + + DEBUG("option.args", " it's an option string"); char * name = *i + 2; char * value = NULL; if (char * p = std::strchr(name, '=')) { *p++ = '\0'; value = p; + DEBUG("option.args", " read option value from option: " << value); } op_bool_tuple opt(find_option(scope, name)); @@ -172,6 +177,7 @@ void process_arguments(int, char ** argv, const bool anywhere, if (opt.get<1>() && value == NULL) { value = *++i; + DEBUG("option.args", " read option value from arg: " << value); if (value == NULL) throw_(option_error, "missing option argument for --" << name); } @@ -181,6 +187,8 @@ void process_arguments(int, char ** argv, const bool anywhere, throw_(option_error, "illegal option -"); } else { + DEBUG("option.args", " single-char option"); + typedef tuple op_bool_char_tuple; std::list option_queue; @@ -199,6 +207,7 @@ void process_arguments(int, char ** argv, const bool anywhere, char * value = NULL; if (o.get<1>()) { value = *++i; + DEBUG("option.args", " read option value from arg: " << value); if (value == NULL) throw_(option_error, "missing option argument for -" << o.get<2>()); -- cgit v1.2.3