diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2014-08-13T01·50+0200 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2014-08-13T01·50+0200 |
commit | 47e185847e729d49e6aa376e8299fd66ef834a0a (patch) | |
tree | 6abfc9e9d5744e78b123c73182c30253c9a39681 /src/libexpr/common-opts.cc | |
parent | 5bed74d1b0acd8d8083fb82a31d907ad2348a91b (diff) |
Refactor option handling
Diffstat (limited to 'src/libexpr/common-opts.cc')
-rw-r--r-- | src/libexpr/common-opts.cc | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/src/libexpr/common-opts.cc b/src/libexpr/common-opts.cc index a3ea202e889e..2e8bb29c7dc2 100644 --- a/src/libexpr/common-opts.cc +++ b/src/libexpr/common-opts.cc @@ -6,40 +6,45 @@ namespace nix { -bool parseOptionArg(const string & arg, Strings::iterator & i, - const Strings::iterator & argsEnd, EvalState & state, - Bindings & autoArgs) +bool parseAutoArgs(Strings::iterator & i, + const Strings::iterator & argsEnd, std::map<string, string> & res) { + string arg = *i; if (arg != "--arg" && arg != "--argstr") return false; UsageError error(format("`%1%' requires two arguments") % arg); - if (i == argsEnd) throw error; - string name = *i++; - if (i == argsEnd) throw error; - string value = *i++; + if (++i == argsEnd) throw error; + string name = *i; + if (++i == argsEnd) throw error; + string value = *i; - /* !!! check for duplicates! */ - Value * v = state.allocValue(); - autoArgs.push_back(Attr(state.symbols.create(name), v)); + res[name] = (arg == "--arg" ? 'E' : 'S') + value; - if (arg == "--arg") - state.mkThunk_(*v, state.parseExprFromString(value, absPath("."))); - else - mkString(*v, value); + return true; +} - autoArgs.sort(); // !!! inefficient - return true; +void evalAutoArgs(EvalState & state, std::map<string, string> & in, Bindings & out) +{ + for (auto & i: in) { + Value * v = state.allocValue(); + if (i.second[0] == 'E') + state.mkThunk_(*v, state.parseExprFromString(string(i.second, 1), absPath("."))); + else + mkString(*v, string(i.second, 1)); + out.push_back(Attr(state.symbols.create(i.first), v)); + } + out.sort(); } -bool parseSearchPathArg(const string & arg, Strings::iterator & i, +bool parseSearchPathArg(Strings::iterator & i, const Strings::iterator & argsEnd, Strings & searchPath) { - if (arg != "-I") return false; - if (i == argsEnd) throw UsageError(format("`%1%' requires an argument") % arg);; - searchPath.push_back(*i++); + if (*i != "-I") return false; + if (++i == argsEnd) throw UsageError("`-I' requires an argument"); + searchPath.push_back(*i); return true; } |