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:19:24 +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-12 01:30:05 +01:00
|
|
|
options:
|
|
|
|
let
|
|
|
|
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
|
|
|
|
then renderSwitch
|
|
|
|
else if builtins.isList value
|
|
|
|
then lib.concatMapStrings renderOption value
|
|
|
|
else renderOption value;
|
|
|
|
|
|
|
|
in
|
|
|
|
lib.concatStrings (lib.mapAttrsToList render options);
|
|
|
|
}
|