From e418976107ed1581c108c82cd5b3b06c2f4ba9db Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sun, 14 Jan 2007 12:32:44 +0000 Subject: [PATCH] * Option --argstr for passing string arguments easily. (NIX-75) --- doc/manual/release-notes.xml | 3 +++ src/libexpr/Makefile.am | 4 ++-- src/libexpr/common-opts.cc | 32 ++++++++++++++++++++++++++ src/libexpr/common-opts.hh | 17 ++++++++++++++ src/libmain/shared.hh | 2 +- src/nix-env/nix-env.cc | 23 +++++++----------- src/nix-instantiate/nix-instantiate.cc | 12 +++------- tests/lang.sh | 6 ++++- tests/lang/eval-okay-autoargs.exp | 1 + tests/lang/eval-okay-autoargs.flags | 1 + tests/lang/eval-okay-autoargs.nix | 15 ++++++++++++ 11 files changed, 88 insertions(+), 28 deletions(-) create mode 100644 src/libexpr/common-opts.cc create mode 100644 src/libexpr/common-opts.hh create mode 100644 tests/lang/eval-okay-autoargs.exp create mode 100644 tests/lang/eval-okay-autoargs.flags create mode 100644 tests/lang/eval-okay-autoargs.nix diff --git a/doc/manual/release-notes.xml b/doc/manual/release-notes.xml index ba36d949d..e32bc2daa 100644 --- a/doc/manual/release-notes.xml +++ b/doc/manual/release-notes.xml @@ -50,6 +50,9 @@ . + TODO: . + + TODO: new built-ins builtins.attrNames, builtins.filterSource. diff --git a/src/libexpr/Makefile.am b/src/libexpr/Makefile.am index 3d255d6bb..b08c079f4 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 000000000..9e3f8f961 --- /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 000000000..fb9659cdc --- /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 fa45645fe..a7e46c927 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 e131bf96c..04641697f 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 b49b84e36..3822de5c6 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"); diff --git a/tests/lang.sh b/tests/lang.sh index 851d89136..18eb0278c 100644 --- a/tests/lang.sh +++ b/tests/lang.sh @@ -40,7 +40,11 @@ for i in lang/eval-okay-*.nix; do i=$(basename $i .nix) if test -e lang/$i.exp; then - if ! $nixinstantiate --eval-only lang/$i.nix > lang/$i.out; then + flags= + if test -e lang/$i.flags; then + flags=$(cat lang/$i.flags) + fi + if ! $nixinstantiate $flags --eval-only lang/$i.nix > lang/$i.out; then echo "FAIL: $i should evaluate" fail=1 elif ! $aterm_bin/atdiff lang/$i.out lang/$i.exp; then diff --git a/tests/lang/eval-okay-autoargs.exp b/tests/lang/eval-okay-autoargs.exp new file mode 100644 index 000000000..3b12bdd5e --- /dev/null +++ b/tests/lang/eval-okay-autoargs.exp @@ -0,0 +1 @@ +Str("xyzzy!xyzzy!foobar",[]) diff --git a/tests/lang/eval-okay-autoargs.flags b/tests/lang/eval-okay-autoargs.flags new file mode 100644 index 000000000..ae3762254 --- /dev/null +++ b/tests/lang/eval-okay-autoargs.flags @@ -0,0 +1 @@ +--arg lib import(lang/lib.nix) --argstr xyzzy xyzzy! -A result diff --git a/tests/lang/eval-okay-autoargs.nix b/tests/lang/eval-okay-autoargs.nix new file mode 100644 index 000000000..1356bb5aa --- /dev/null +++ b/tests/lang/eval-okay-autoargs.nix @@ -0,0 +1,15 @@ +let + + foobar = "foobar"; + +in + +{ xyzzy2 ? xyzzy # mutually recursive args +, xyzzy ? "blaat" # will be overriden by --argstr +, fb ? foobar +, lib # will be set by --arg +}: + +{ + result = lib.concat [xyzzy xyzzy2 fb]; +}