summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/cli.nix26
-rw-r--r--lib/tests/misc.nix29
2 files changed, 51 insertions, 4 deletions
diff --git a/lib/cli.nix b/lib/cli.nix
index f47625d2f537..32d24a00ceb0 100644
--- a/lib/cli.nix
+++ b/lib/cli.nix
@@ -6,8 +6,29 @@ rec {
This helps protect against malformed command lines and also to reduce
boilerplate related to command-line construction for simple use cases.
+ `toGNUCommandLine` returns a list of nix strings.
+ `toGNUCommandLineShell` returns an escaped shell string.
+
Example:
- encodeGNUCommandLine
+ toGNUCommandLine
+ { }
+ { data = builtins.toJSON { id = 0; };
+
+ X = "PUT";
+
+ retry = 3;
+
+ retry-delay = null;
+
+ url = [ "https://example.com/foo" "https://example.com/bar" ];
+
+ silent = false;
+
+ verbose = true;
+ };
+ => [ "-X" "PUT" "--data" "{\"id\":0}" "--retry" "3" "--url" "https://example.com/foo" "--url" "https://example.com/bar" "--verbose" ]
+
+ toGNUCommandLineShell
{ }
{ data = builtins.toJSON { id = 0; };
@@ -24,8 +45,9 @@ rec {
verbose = true;
};
=> "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'"
+
*/
- encodeGNUCommandLine =
+ toGNUCommandLineShell =
options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs);
toGNUCommandLine =
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index e47b48b5017d..b320839b2ac7 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -441,9 +441,34 @@ runTests {
expected = "«foo»";
};
- testRenderOptions = {
+
+# CLI
+
+ testToGNUCommandLine = {
+ expr =
+ cli.toGNUCommandLine
+ { }
+ { data = builtins.toJSON { id = 0; };
+
+ X = "PUT";
+
+ retry = 3;
+
+ retry-delay = null;
+
+ url = [ "https://example.com/foo" "https://example.com/bar" ];
+
+ silent = false;
+
+ verbose = true;
+ };
+
+ expected = [ "-X" "PUT" "--data" "{\"id\":0}" "--retry" "3" "--url" "https://example.com/foo" "--url" "https://example.com/bar" "--verbose" ];
+ };
+
+ testToGNUCommandLineShell = {
expr =
- encodeGNUCommandLine
+ cli.toGNUCommandLineShell
{ }
{ data = builtins.toJSON { id = 0; };