mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-22 15:03:28 +00:00
Make behavior of encodeGNUCommandLine
customizable
... based on feedback from @edolstra
This commit is contained in:
parent
8c6a05c8c9
commit
693096d283
30
lib/cli.nix
30
lib/cli.nix
@ -6,27 +6,31 @@
|
|||||||
boilerplate related to command-line construction for simple use cases.
|
boilerplate related to command-line construction for simple use cases.
|
||||||
|
|
||||||
Example:
|
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"
|
=> " --bar '1' --foo 'A' --qux -v"
|
||||||
*/
|
*/
|
||||||
encodeGNUCommandLine =
|
encodeGNUCommandLine =
|
||||||
|
{ renderKey ?
|
||||||
|
key: if builtins.stringLength key == 1 then "-${key}" else "--${key}"
|
||||||
|
|
||||||
|
, renderOption ?
|
||||||
|
key: value:
|
||||||
|
if value == null
|
||||||
|
then ""
|
||||||
|
else " ${renderKey key} ${lib.escapeShellArg value}"
|
||||||
|
|
||||||
|
, renderBool ? key: value: if value then " ${renderKey key}" else ""
|
||||||
|
|
||||||
|
, renderList ? key: value: lib.concatMapStrings renderOption value
|
||||||
|
}:
|
||||||
options:
|
options:
|
||||||
let
|
let
|
||||||
render = key: value:
|
render = key: value:
|
||||||
let
|
|
||||||
hyphenate =
|
|
||||||
k: if builtins.stringLength k == 1 then "-${k}" else "--${k}";
|
|
||||||
|
|
||||||
renderOption = v: if v == null then "" else " ${hyphenate key} ${lib.escapeShellArg v}";
|
|
||||||
|
|
||||||
renderSwitch = if value then " ${hyphenate key}" else "";
|
|
||||||
|
|
||||||
in
|
|
||||||
if builtins.isBool value
|
if builtins.isBool value
|
||||||
then renderSwitch
|
then renderBool key value
|
||||||
else if builtins.isList value
|
else if builtins.isList value
|
||||||
then lib.concatMapStrings renderOption value
|
then renderList key value
|
||||||
else renderOption value;
|
else renderOption key value;
|
||||||
|
|
||||||
in
|
in
|
||||||
lib.concatStrings (lib.mapAttrsToList render options);
|
lib.concatStrings (lib.mapAttrsToList render options);
|
||||||
|
@ -444,6 +444,7 @@ runTests {
|
|||||||
testRenderOptions = {
|
testRenderOptions = {
|
||||||
expr =
|
expr =
|
||||||
encodeGNUCommandLine
|
encodeGNUCommandLine
|
||||||
|
{ }
|
||||||
{ foo = "A";
|
{ foo = "A";
|
||||||
|
|
||||||
bar = 1;
|
bar = 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user