4 /* Automatically convert an attribute set to command-line options.
6 This helps protect against malformed command lines and also to reduce
7 boilerplate related to command-line construction for simple use cases.
9 `toGNUCommandLine` returns a list of nix strings.
10 `toGNUCommandLineShell` returns an escaped shell string.
13 cli.toGNUCommandLine {} {
14 data = builtins.toJSON { id = 0; };
18 url = [ "https://example.com/foo" "https://example.com/bar" ];
26 "--url" "https://example.com/foo"
27 "--url" "https://example.com/bar"
31 cli.toGNUCommandLineShell {} {
32 data = builtins.toJSON { id = 0; };
36 url = [ "https://example.com/foo" "https://example.com/bar" ];
40 => "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'";
42 toGNUCommandLineShell =
43 options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs);
46 # how to string-format the option name;
47 # by default one character is a short option (`-`),
48 # more than one characters a long option (`--`).
50 k: if builtins.stringLength k == 1
54 # how to format a boolean value to a command list;
55 # by default it’s a flag option
56 # (only the option name if true, left out completely if false).
57 mkBool ? k: v: lib.optional v (mkOptionName k),
59 # how to format a list value to a command list;
60 # by default the option name is repeated for each value
61 # and `mkOption` is applied to the values themselves.
62 mkList ? k: v: lib.concatMap (mkOption k) v,
64 # how to format any remaining value to a command list;
65 # on the toplevel, booleans and lists are handled by `mkBool` and `mkList`,
66 # though they can still appear as values of a list.
67 # By default, everything is printed verbatim and complex types
68 # are forbidden (lists, attrsets, functions). `null` values are omitted.
72 else [ (mkOptionName k) (lib.generators.mkValueStringDefault {} v) ]
77 if builtins.isBool v then mkBool k v
78 else if builtins.isList v then mkList k v
82 builtins.concatLists (lib.mapAttrsToList render options);