about summary refs log tree commit diff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2007-01-14T12·32+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2007-01-14T12·32+0000
commite418976107ed1581c108c82cd5b3b06c2f4ba9db (patch)
treee4346ab680095c66ecd49610548b18aa10c33a04 /src/libexpr
parent4e329f173f738dfc83e58bdb8ad2ced6c452a395 (diff)
* Option --argstr for passing string arguments easily. (NIX-75)
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/Makefile.am4
-rw-r--r--src/libexpr/common-opts.cc32
-rw-r--r--src/libexpr/common-opts.hh17
3 files changed, 51 insertions, 2 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 */