From 47e185847e729d49e6aa376e8299fd66ef834a0a Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 13 Aug 2014 03:50:44 +0200 Subject: Refactor option handling --- src/libexpr/common-opts.cc | 45 +++++++++++++++++++++++++-------------------- src/libexpr/common-opts.hh | 9 +++++---- 2 files changed, 30 insertions(+), 24 deletions(-) (limited to 'src/libexpr') 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 & 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 & 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; } diff --git a/src/libexpr/common-opts.hh b/src/libexpr/common-opts.hh index 759358950f0a..bb6d399a8a61 100644 --- a/src/libexpr/common-opts.hh +++ b/src/libexpr/common-opts.hh @@ -5,11 +5,12 @@ namespace nix { /* Some common option parsing between nix-env and nix-instantiate. */ -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 & res); -bool parseSearchPathArg(const string & arg, Strings::iterator & i, +void evalAutoArgs(EvalState & state, std::map & in, Bindings & out); + +bool parseSearchPathArg(Strings::iterator & i, const Strings::iterator & argsEnd, Strings & searchPath); Path lookupFileArg(EvalState & state, string s); -- cgit 1.4.1