2019-12-12 01:30:05 +01:00
|
|
|
{ lib }:
|
|
|
|
|
|
|
|
{ /* Automatically convert an attribute set to command-line options.
|
|
|
|
|
|
|
|
This helps protect against malformed command lines and also to reduce
|
|
|
|
boilerplate related to command-line construction for simple use cases.
|
|
|
|
|
|
|
|
Example:
|
2019-12-14 03:25:52 +01:00
|
|
|
encodeGNUCommandLine { } { foo = "A"; bar = 1; baz = null; qux = true; v = true; }
|
2019-12-12 01:30:05 +01:00
|
|
|
=> " --bar '1' --foo 'A' --qux -v"
|
|
|
|
*/
|
2019-12-14 03:19:24 +01:00
|
|
|
encodeGNUCommandLine =
|
2019-12-14 03:25:52 +01:00
|
|
|
{ renderKey ?
|
|
|
|
key: if builtins.stringLength key == 1 then "-${key}" else "--${key}"
|
2019-12-12 01:30:05 +01:00
|
|
|
|
2019-12-14 03:25:52 +01:00
|
|
|
, renderOption ?
|
|
|
|
key: value:
|
|
|
|
if value == null
|
|
|
|
then ""
|
|
|
|
else " ${renderKey key} ${lib.escapeShellArg value}"
|
2019-12-12 01:30:05 +01:00
|
|
|
|
2019-12-14 03:25:52 +01:00
|
|
|
, renderBool ? key: value: if value then " ${renderKey key}" else ""
|
2019-12-12 01:30:05 +01:00
|
|
|
|
2019-12-14 03:25:52 +01:00
|
|
|
, renderList ? key: value: lib.concatMapStrings renderOption value
|
|
|
|
}:
|
|
|
|
options:
|
|
|
|
let
|
|
|
|
render = key: value:
|
2019-12-12 01:30:05 +01:00
|
|
|
if builtins.isBool value
|
2019-12-14 03:25:52 +01:00
|
|
|
then renderBool key value
|
2019-12-12 01:30:05 +01:00
|
|
|
else if builtins.isList value
|
2019-12-14 03:25:52 +01:00
|
|
|
then renderList key value
|
|
|
|
else renderOption key value;
|
2019-12-12 01:30:05 +01:00
|
|
|
|
|
|
|
in
|
|
|
|
lib.concatStrings (lib.mapAttrsToList render options);
|
|
|
|
}
|