diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libexpr/Makefile.am | 4 | ||||
-rw-r--r-- | src/libexpr/common-opts.cc | 32 | ||||
-rw-r--r-- | src/libexpr/common-opts.hh | 17 | ||||
-rw-r--r-- | src/libmain/shared.hh | 2 | ||||
-rw-r--r-- | src/nix-env/nix-env.cc | 23 | ||||
-rw-r--r-- | src/nix-instantiate/nix-instantiate.cc | 12 |
6 files changed, 63 insertions, 27 deletions
diff --git a/src/libexpr/Makefile.am b/src/libexpr/Makefile.am index 3d255d6bbd32..b08c079f4d30 100644 --- a/src/libexpr/Makefile.am +++ b/src/libexpr/Makefile.am @@ -2,11 +2,11 @@ pkglib_LTLIBRARIES = libexpr.la libexpr_la_SOURCES = \ nixexpr.cc eval.cc primops.cc lexer-tab.cc parser-tab.cc \ - get-drvs.cc attr-path.cc expr-to-xml.cc + get-drvs.cc attr-path.cc expr-to-xml.cc common-opts.cc pkginclude_HEADERS = \ nixexpr.hh eval.hh parser.hh lexer-tab.hh parser-tab.hh \ - get-drvs.hh attr-path.hh expr-to-xml.hh + get-drvs.hh attr-path.hh expr-to-xml.hh common-opts.hh libexpr_la_LIBADD = ../libutil/libutil.la ../libstore/libstore.la \ ../boost/format/libformat.la diff --git a/src/libexpr/common-opts.cc b/src/libexpr/common-opts.cc new file mode 100644 index 000000000000..9e3f8f9614da --- /dev/null +++ b/src/libexpr/common-opts.cc @@ -0,0 +1,32 @@ +#include "common-opts.hh" +#include "../libmain/shared.hh" +#include "util.hh" +#include "parser.hh" + + +namespace nix { + + +bool parseOptionArg(const string & arg, Strings::iterator & i, + const Strings::iterator & argsEnd, EvalState & state, + ATermMap & autoArgs) +{ + 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++; + + Expr e = arg == "--arg" + ? parseExprFromString(state, value, absPath(".")) + : makeStr(value); + autoArgs.set(toATerm(name), e); + + return true; +} + + +} diff --git a/src/libexpr/common-opts.hh b/src/libexpr/common-opts.hh new file mode 100644 index 000000000000..fb9659cdc5a1 --- /dev/null +++ b/src/libexpr/common-opts.hh @@ -0,0 +1,17 @@ +#ifndef __COMMON_OPTS_H +#define __COMMON_OPTS_H + +#include "eval.hh" + + +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, + ATermMap & autoArgs); + +} + + +#endif /* !__COMMON_OPTS_H */ diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh index fa45645fef40..a7e46c927d08 100644 --- a/src/libmain/shared.hh +++ b/src/libmain/shared.hh @@ -31,7 +31,7 @@ extern bool setuidMode; extern volatile ::sig_atomic_t blockInt; -MakeError(UsageError, nix::Error) +MakeError(UsageError, nix::Error); } diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index e131bf96cf49..04641697f1bd 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -10,6 +10,7 @@ #include "get-drvs.hh" #include "attr-path.hh" #include "pathlocks.hh" +#include "common-opts.hh" #include "xml-writer.hh" #include "store-api.hh" #include "db.hh" @@ -45,7 +46,7 @@ struct InstallSourceInfo Path profile; /* for srcProfile */ string systemFilter; /* for srcNixExprDrvs */ ATermMap autoArgs; - InstallSourceInfo() : autoArgs(128) { }; + InstallSourceInfo() : autoArgs() { }; }; @@ -1122,10 +1123,9 @@ static void opDefaultExpr(Globals & globals, static string needArg(Strings::iterator & i, Strings & args, const string & arg) { - ++i; if (i == args.end()) throw UsageError( format("`%1%' requires an argument") % arg); - return *i; + return *i++; } @@ -1146,8 +1146,8 @@ void run(Strings args) globals.keepDerivations = queryBoolSetting("env-keep-derivations", false); - for (Strings::iterator i = args.begin(); i != args.end(); ++i) { - string arg = *i; + for (Strings::iterator i = args.begin(); i != args.end(); ) { + string arg = *i++; Operation oldOp = op; @@ -1161,16 +1161,9 @@ void run(Strings args) } else if (arg == "--attr" || arg == "-A") globals.instSource.type = srcAttrPath; - else if (arg == "--arg") { /* !!! code duplication from nix-instantiate */ - i++; - if (i == args.end()) - throw UsageError("`--arg' requires two arguments"); - string name = *i++; - if (i == args.end()) - throw UsageError("`--arg' requires two arguments"); - Expr value = parseExprFromString(globals.state, *i, absPath(".")); - globals.instSource.autoArgs.set(toATerm(name), value); - } + else if (parseOptionArg(arg, i, args.end(), + globals.state, globals.instSource.autoArgs)) + ; else if (arg == "--force-name") // undocumented flag for nix-install-package globals.forceName = needArg(i, args, arg); else if (arg == "--uninstall" || arg == "-e") diff --git a/src/nix-instantiate/nix-instantiate.cc b/src/nix-instantiate/nix-instantiate.cc index b49b84e36ae2..3822de5c6289 100644 --- a/src/nix-instantiate/nix-instantiate.cc +++ b/src/nix-instantiate/nix-instantiate.cc @@ -10,6 +10,7 @@ #include "expr-to-xml.hh" #include "util.hh" #include "store-api.hh" +#include "common-opts.hh" #include "help.txt.hh" @@ -112,15 +113,8 @@ void run(Strings args) throw UsageError("`--attr' requires an argument"); attrPaths.push_back(*i++); } - else if (arg == "--arg") { - if (i == args.end()) - throw UsageError("`--arg' requires two arguments"); - string name = *i++; - if (i == args.end()) - throw UsageError("`--arg' requires two arguments"); - Expr value = parseExprFromString(state, *i++, absPath(".")); - autoArgs.set(toATerm(name), value); - } + else if (parseOptionArg(arg, i, args.end(), state, autoArgs)) + ; else if (arg == "--add-root") { if (i == args.end()) throw UsageError("`--add-root' requires an argument"); |