about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-13T02·03+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-13T02·08+0200
commit5f05197df460bafd9a4f451d69757401b35a1180 (patch)
tree7f2f3798ae572474684260d498ad26c0166c3b69 /src
parent47e185847e729d49e6aa376e8299fd66ef834a0a (diff)
Handle compound single dash options properly
So now

  nix-instantiate --eval -E '{x}: x' --argstr x -xyzzy

correctly prints "-xyzzy", rather than giving an error.

Issue NixOS/hydra#176.
Diffstat (limited to 'src')
-rw-r--r--src/libmain/shared.cc29
1 files changed, 14 insertions, 15 deletions
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc
index ec05db0a6675..9ac9d2773852 100644
--- a/src/libmain/shared.cc
+++ b/src/libmain/shared.cc
@@ -159,29 +159,28 @@ void parseCmdLine(int argc, char * * argv,
     std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg)
 {
     /* Put the arguments in a vector. */
-    Strings args, remaining;
+    Strings args;
+    argc--; argv++;
     while (argc--) args.push_back(*argv++);
-    args.erase(args.begin());
 
-    /* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'), and
-       ignore options for the ATerm library. */
+    /* Process default options. */
     for (Strings::iterator i = args.begin(); i != args.end(); ++i) {
         string arg = *i;
-        if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-' && !isdigit(arg[1])) {
-            for (unsigned int j = 1; j < arg.length(); j++)
+
+        /* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'). */
+        if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-' && isalpha(arg[1])) {
+            *i = (string) "-" + arg[1];
+            auto next = i; ++next;
+            for (unsigned int j = 2; j < arg.length(); j++)
                 if (isalpha(arg[j]))
-                    remaining.push_back((string) "-" + arg[j]);
-                else     {
-                    remaining.push_back(string(arg, j));
+                    args.insert(next, (string) "-" + arg[j]);
+                else {
+                    args.insert(next, string(arg, j));
                     break;
                 }
-        } else remaining.push_back(arg);
-    }
-    args = remaining;
+            arg = *i;
+        }
 
-    /* Process default options. */
-    for (Strings::iterator i = args.begin(); i != args.end(); ++i) {
-        string arg = *i;
         if (arg == "--verbose" || arg == "-v") verbosity = (Verbosity) (verbosity + 1);
         else if (arg == "--quiet") verbosity = verbosity > lvlError ? (Verbosity) (verbosity - 1) : lvlError;
         else if (arg == "--log-type") {