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.
17 : How to format the arguments, see `toGNUCommandLine`
21 : The attributes to transform into arguments.
26 ## `lib.cli.toGNUCommandLineShell` usage example
29 cli.toGNUCommandLineShell {} {
30 data = builtins.toJSON { id = 0; };
34 url = [ "https://example.com/foo" "https://example.com/bar" ];
38 => "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'";
43 toGNUCommandLineShell =
44 options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs);
47 Automatically convert an attribute set to a list of command-line options.
49 `toGNUCommandLine` returns a list of string arguments.
56 : How to format the arguments, see below.
60 : The attributes to transform into arguments.
66 : How to string-format the option name;
67 By default one character is a short option (`-`), more than one characters a long option (`--`).
71 : How to format a boolean value to a command list;
72 By default it’s a flag option (only the option name if true, left out completely if false).
76 : How to format a list value to a command list;
77 By default the option name is repeated for each value and `mkOption` is applied to the values themselves.
82 : How to format any remaining value to a command list;
83 On the toplevel, booleans and lists are handled by `mkBool` and `mkList`, though they can still appear as values of a list.
84 By default, everything is printed verbatim and complex types are forbidden (lists, attrsets, functions). `null` values are omitted.
86 `optionValueSeparator`
88 : How to separate an option from its flag;
89 By default, there is no separator, so option `-c` and value `5` would become ["-c" "5"].
90 This is useful if the command requires equals, for example, `-c=5`.
95 ## `lib.cli.toGNUCommandLine` usage example
98 cli.toGNUCommandLine {} {
99 data = builtins.toJSON { id = 0; };
103 url = [ "https://example.com/foo" "https://example.com/bar" ];
109 "--data" "{\"id\":0}"
111 "--url" "https://example.com/foo"
112 "--url" "https://example.com/bar"
121 k: if builtins.stringLength k == 1
125 mkBool ? k: v: lib.optional v (mkOptionName k),
127 mkList ? k: v: lib.concatMap (mkOption k) v,
132 else if optionValueSeparator == null then
133 [ (mkOptionName k) (lib.generators.mkValueStringDefault {} v) ]
135 [ "${mkOptionName k}${optionValueSeparator}${lib.generators.mkValueStringDefault {} v}" ],
137 optionValueSeparator ? null
142 if builtins.isBool v then mkBool k v
143 else if builtins.isList v then mkList k v
147 builtins.concatLists (lib.mapAttrsToList render options);