* Option --argstr for passing string arguments easily. (NIX-75)

This commit is contained in:
Eelco Dolstra 2007-01-14 12:32:44 +00:00
parent 4e329f173f
commit e418976107
11 changed files with 88 additions and 28 deletions

View file

@ -50,6 +50,9 @@
<option>--set</option>.</para></listitem> <option>--set</option>.</para></listitem>
<listitem><para>TODO: <option>--argstr</option>.</para></listitem>
<listitem><para>TODO: new built-ins <listitem><para>TODO: new built-ins
<function>builtins.attrNames</function>, <function>builtins.attrNames</function>,
<function>builtins.filterSource</function>.</para></listitem> <function>builtins.filterSource</function>.</para></listitem>

View file

@ -2,11 +2,11 @@ pkglib_LTLIBRARIES = libexpr.la
libexpr_la_SOURCES = \ libexpr_la_SOURCES = \
nixexpr.cc eval.cc primops.cc lexer-tab.cc parser-tab.cc \ 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 = \ pkginclude_HEADERS = \
nixexpr.hh eval.hh parser.hh lexer-tab.hh parser-tab.hh \ 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 \ libexpr_la_LIBADD = ../libutil/libutil.la ../libstore/libstore.la \
../boost/format/libformat.la ../boost/format/libformat.la

View file

@ -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;
}
}

View file

@ -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 */

View file

@ -31,7 +31,7 @@ extern bool setuidMode;
extern volatile ::sig_atomic_t blockInt; extern volatile ::sig_atomic_t blockInt;
MakeError(UsageError, nix::Error) MakeError(UsageError, nix::Error);
} }

View file

@ -10,6 +10,7 @@
#include "get-drvs.hh" #include "get-drvs.hh"
#include "attr-path.hh" #include "attr-path.hh"
#include "pathlocks.hh" #include "pathlocks.hh"
#include "common-opts.hh"
#include "xml-writer.hh" #include "xml-writer.hh"
#include "store-api.hh" #include "store-api.hh"
#include "db.hh" #include "db.hh"
@ -45,7 +46,7 @@ struct InstallSourceInfo
Path profile; /* for srcProfile */ Path profile; /* for srcProfile */
string systemFilter; /* for srcNixExprDrvs */ string systemFilter; /* for srcNixExprDrvs */
ATermMap autoArgs; ATermMap autoArgs;
InstallSourceInfo() : autoArgs(128) { }; InstallSourceInfo() : autoArgs() { };
}; };
@ -1122,10 +1123,9 @@ static void opDefaultExpr(Globals & globals,
static string needArg(Strings::iterator & i, static string needArg(Strings::iterator & i,
Strings & args, const string & arg) Strings & args, const string & arg)
{ {
++i;
if (i == args.end()) throw UsageError( if (i == args.end()) throw UsageError(
format("`%1%' requires an argument") % arg); format("`%1%' requires an argument") % arg);
return *i; return *i++;
} }
@ -1146,8 +1146,8 @@ void run(Strings args)
globals.keepDerivations = globals.keepDerivations =
queryBoolSetting("env-keep-derivations", false); queryBoolSetting("env-keep-derivations", false);
for (Strings::iterator i = args.begin(); i != args.end(); ++i) { for (Strings::iterator i = args.begin(); i != args.end(); ) {
string arg = *i; string arg = *i++;
Operation oldOp = op; Operation oldOp = op;
@ -1161,16 +1161,9 @@ void run(Strings args)
} }
else if (arg == "--attr" || arg == "-A") else if (arg == "--attr" || arg == "-A")
globals.instSource.type = srcAttrPath; globals.instSource.type = srcAttrPath;
else if (arg == "--arg") { /* !!! code duplication from nix-instantiate */ else if (parseOptionArg(arg, i, args.end(),
i++; globals.state, globals.instSource.autoArgs))
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 (arg == "--force-name") // undocumented flag for nix-install-package else if (arg == "--force-name") // undocumented flag for nix-install-package
globals.forceName = needArg(i, args, arg); globals.forceName = needArg(i, args, arg);
else if (arg == "--uninstall" || arg == "-e") else if (arg == "--uninstall" || arg == "-e")

View file

@ -10,6 +10,7 @@
#include "expr-to-xml.hh" #include "expr-to-xml.hh"
#include "util.hh" #include "util.hh"
#include "store-api.hh" #include "store-api.hh"
#include "common-opts.hh"
#include "help.txt.hh" #include "help.txt.hh"
@ -112,15 +113,8 @@ void run(Strings args)
throw UsageError("`--attr' requires an argument"); throw UsageError("`--attr' requires an argument");
attrPaths.push_back(*i++); attrPaths.push_back(*i++);
} }
else if (arg == "--arg") { else if (parseOptionArg(arg, i, args.end(), state, autoArgs))
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 (arg == "--add-root") { else if (arg == "--add-root") {
if (i == args.end()) if (i == args.end())
throw UsageError("`--add-root' requires an argument"); throw UsageError("`--add-root' requires an argument");

View file

@ -40,7 +40,11 @@ for i in lang/eval-okay-*.nix; do
i=$(basename $i .nix) i=$(basename $i .nix)
if test -e lang/$i.exp; then 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" echo "FAIL: $i should evaluate"
fail=1 fail=1
elif ! $aterm_bin/atdiff lang/$i.out lang/$i.exp; then elif ! $aterm_bin/atdiff lang/$i.out lang/$i.exp; then

View file

@ -0,0 +1 @@
Str("xyzzy!xyzzy!foobar",[])

View file

@ -0,0 +1 @@
--arg lib import(lang/lib.nix) --argstr xyzzy xyzzy! -A result

View file

@ -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];
}