From 693096d283763ce71fcd2002d965c07546aaafda Mon Sep 17 00:00:00 2001 From: Gabriel Gonzalez Date: Fri, 13 Dec 2019 18:25:52 -0800 Subject: Make behavior of `encodeGNUCommandLine` customizable ... based on feedback from @edolstra --- lib/cli.nix | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'lib/cli.nix') 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); -- cgit 1.4.1