diff options
author | lassulus <lassulus@lassul.us> | 2017-09-19 22:56:19 +0200 |
---|---|---|
committer | lassulus <lassulus@lassul.us> | 2017-09-19 22:56:19 +0200 |
commit | 821e59baca98b5caba65c6e7c7e4d6671593a69e (patch) | |
tree | 499e78ef143bf20c7cb2ef4137d031085d25c6a9 /krebs/5pkgs/simple/withGetopt.nix | |
parent | d30480f1457cf4ac4f67f5e02faec75765706e50 (diff) | |
parent | 94c57badae775cb863b76a6c6cb8c11012cd4f83 (diff) |
Merge remote-tracking branch 'ni/master'
Diffstat (limited to 'krebs/5pkgs/simple/withGetopt.nix')
-rw-r--r-- | krebs/5pkgs/simple/withGetopt.nix | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/krebs/5pkgs/simple/withGetopt.nix b/krebs/5pkgs/simple/withGetopt.nix index fd59adcaf..196e6765a 100644 --- a/krebs/5pkgs/simple/withGetopt.nix +++ b/krebs/5pkgs/simple/withGetopt.nix @@ -13,6 +13,7 @@ opt-spec: cmd-spec: let opts = mapAttrs (name: value: value // rec { long = value.long or (replaceStrings ["_"] ["-"] name); ref = value.ref or "\"\$${varname}\""; + short = value.short or null; switch = value.switch or false; varname = value.varname or (replaceStrings ["-"] ["_"] name); }) opt-spec; @@ -43,11 +44,17 @@ in writeDash wrapper-name '' '') opts)} args=$(${utillinux}/bin/getopt \ + -l ${shell.escape + (concatMapStringsSep "," + (opt: opt.long + optionalString (!opt.switch) ":") + (filter (opt: opt.long != null) + (attrValues opts)))} \ -n "$wrapper_name" \ - -o "" \ - -l ${concatMapStringsSep "," - (opt: opt.long + optionalString (!opt.switch) ":") - (attrValues opts)} \ + -o ${shell.escape + (concatMapStringsSep "" + (opt: opt.short + optionalString (!opt.switch) ":") + (filter (opt: opt.short != null) + (attrValues opts)))} \ -s sh \ -- "$@") if \test $? != 0; then exit 1; fi @@ -56,7 +63,10 @@ in writeDash wrapper-name '' while :; do case $1 in ${concatStringsSep "\n" (mapAttrsToList (name: opt: /* sh */ '' - --${opt.long}) + (${concatMapStringsSep "|" shell.escape (filter (x: x != "") [ + (optionalString (opt.long != null) "--${opt.long}") + (optionalString (opt.short != null) "-${opt.short}") + ])}) ${if opt.switch then /* sh */ '' ${opt.varname}=true shift @@ -65,7 +75,9 @@ in writeDash wrapper-name '' shift 2 ''} ;; - '') opts)} + '') (filterAttrs + (_: opt: opt.long != null || opt.short != null) + opts))} --) shift break @@ -102,5 +114,5 @@ in writeDash wrapper-name '' export ${opt.varname} '') opts)} - ${cmd-script} + ${cmd-script} "$@" '' |