summary refs log tree commit diff
path: root/lib/cli.nix
diff options
context:
space:
mode:
authorGabriel Gonzalez <Gabriel439@gmail.com>2019-12-13 18:25:52 -0800
committerGabriel Gonzalez <Gabriel439@gmail.com>2019-12-13 18:25:52 -0800
commit693096d283763ce71fcd2002d965c07546aaafda (patch)
treebdfffbf5715ec0503d3a375dc0a73df100fdffdd /lib/cli.nix
parent8c6a05c8c99819dbd85d555cb50596637d57df44 (diff)
downloadnixpkgs-693096d283763ce71fcd2002d965c07546aaafda.tar
nixpkgs-693096d283763ce71fcd2002d965c07546aaafda.tar.gz
nixpkgs-693096d283763ce71fcd2002d965c07546aaafda.tar.bz2
nixpkgs-693096d283763ce71fcd2002d965c07546aaafda.tar.lz
nixpkgs-693096d283763ce71fcd2002d965c07546aaafda.tar.xz
nixpkgs-693096d283763ce71fcd2002d965c07546aaafda.tar.zst
nixpkgs-693096d283763ce71fcd2002d965c07546aaafda.zip
Make behavior of `encodeGNUCommandLine` customizable
... based on feedback from @edolstra
Diffstat (limited to 'lib/cli.nix')
-rw-r--r--lib/cli.nix30
1 files changed, 17 insertions, 13 deletions
diff --git a/lib/cli.nix b/lib/cli.nix
index 23fab8ec970..f3a81cb9e9c 100644
--- a/lib/cli.nix
+++ b/lib/cli.nix
@@ -6,27 +6,31 @@
      boilerplate related to command-line construction for simple use cases.
 
      Example:
-       encodeGNUCommandLine { foo = "A"; bar = 1; baz = null; qux = true; v = true; }
+       encodeGNUCommandLine { } { foo = "A"; bar = 1; baz = null; qux = true; v = true; }
        => " --bar '1' --foo 'A' --qux -v"
   */
   encodeGNUCommandLine =
-    options:
-      let
-        render = key: value:
-          let
-            hyphenate =
-              k: if builtins.stringLength k == 1 then "-${k}" else "--${k}";
+    { renderKey ?
+        key: if builtins.stringLength key == 1 then "-${key}" else "--${key}"
 
-            renderOption = v: if v == null then "" else " ${hyphenate key} ${lib.escapeShellArg v}";
+    , renderOption ?
+        key: value:
+          if value == null
+          then ""
+          else " ${renderKey key} ${lib.escapeShellArg value}"
 
-            renderSwitch = if value then " ${hyphenate key}" else "";
+    , renderBool ? key: value: if value then " ${renderKey key}" else ""
 
-          in
+    , renderList ? key: value: lib.concatMapStrings renderOption value
+    }:
+    options:
+      let
+        render = key: value:
                  if builtins.isBool value
-            then renderSwitch
+            then renderBool key value
             else if builtins.isList value
-            then lib.concatMapStrings renderOption value
-            else renderOption value;
+            then renderList key value
+            else renderOption key value;
 
       in
         lib.concatStrings (lib.mapAttrsToList render options);