5 Automatically convert an attribute set to command-line options.
7 This helps protect against malformed command lines and also to reduce
8 boilerplate related to command-line construction for simple use cases.
10 `toGNUCommandLineShell` returns an escaped shell string.
16 : How to format the arguments, see `toGNUCommandLine`
20 : The attributes to transform into arguments.
24 ## `lib.cli.toGNUCommandLineShell` usage example
27 cli.toGNUCommandLineShell {} {
28 data = builtins.toJSON { id = 0; };
32 url = [ "https://example.com/foo" "https://example.com/bar" ];
36 => "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'";
41 toGNUCommandLineShell = options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs);
44 Automatically convert an attribute set to a list of command-line options.
46 `toGNUCommandLine` returns a list of string arguments.
52 : How to format the arguments, see below.
56 : The attributes to transform into arguments.
62 : How to string-format the option name;
63 By default one character is a short option (`-`), more than one characters a long option (`--`).
67 : How to format a boolean value to a command list;
68 By default it’s a flag option (only the option name if true, left out completely if false).
72 : How to format a list value to a command list;
73 By default the option name is repeated for each value and `mkOption` is applied to the values themselves.
77 : How to format any remaining value to a command list;
78 On the toplevel, booleans and lists are handled by `mkBool` and `mkList`, though they can still appear as values of a list.
79 By default, everything is printed verbatim and complex types are forbidden (lists, attrsets, functions). `null` values are omitted.
81 `optionValueSeparator`
83 : How to separate an option from its flag;
84 By default, there is no separator, so option `-c` and value `5` would become ["-c" "5"].
85 This is useful if the command requires equals, for example, `-c=5`.
89 ## `lib.cli.toGNUCommandLine` usage example
92 cli.toGNUCommandLine {} {
93 data = builtins.toJSON { id = 0; };
97 url = [ "https://example.com/foo" "https://example.com/bar" ];
103 "--data" "{\"id\":0}"
105 "--url" "https://example.com/foo"
106 "--url" "https://example.com/bar"
115 mkOptionName ? k: if builtins.stringLength k == 1 then "-${k}" else "--${k}",
117 mkBool ? k: v: lib.optional v (mkOptionName k),
119 mkList ? k: v: lib.concatMap (mkOption k) v,
125 else if optionValueSeparator == null then
128 (lib.generators.mkValueStringDefault { } v)
131 [ "${mkOptionName k}${optionValueSeparator}${lib.generators.mkValueStringDefault { } v}" ],
133 optionValueSeparator ? null,
139 if builtins.isBool v then
141 else if builtins.isList v then
147 builtins.concatLists (lib.mapAttrsToList render options);