about summary refs log tree commit diff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-13T01·50+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-13T01·50+0200
commit47e185847e729d49e6aa376e8299fd66ef834a0a (patch)
tree6abfc9e9d5744e78b123c73182c30253c9a39681 /src/libexpr
parent5bed74d1b0acd8d8083fb82a31d907ad2348a91b (diff)
Refactor option handling
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/common-opts.cc45
-rw-r--r--src/libexpr/common-opts.hh9
2 files changed, 30 insertions, 24 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;
 }
 
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<string, string> & res);
 
-bool parseSearchPathArg(const string & arg, Strings::iterator & i,
+void evalAutoArgs(EvalState & state, std::map<string, string> & in, Bindings & out);
+
+bool parseSearchPathArg(Strings::iterator & i,
     const Strings::iterator & argsEnd, Strings & searchPath);
 
 Path lookupFileArg(EvalState & state, string s);