withGetopt: support short options

This commit is contained in:
tv 2017-09-19 20:59:08 +02:00
parent ab7e0c879c
commit 16e6046544

View File

@ -13,6 +13,7 @@ opt-spec: cmd-spec: let
opts = mapAttrs (name: value: value // rec { opts = mapAttrs (name: value: value // rec {
long = value.long or (replaceStrings ["_"] ["-"] name); long = value.long or (replaceStrings ["_"] ["-"] name);
ref = value.ref or "\"\$${varname}\""; ref = value.ref or "\"\$${varname}\"";
short = value.short or null;
switch = value.switch or false; switch = value.switch or false;
varname = value.varname or (replaceStrings ["-"] ["_"] name); varname = value.varname or (replaceStrings ["-"] ["_"] name);
}) opt-spec; }) opt-spec;
@ -38,19 +39,6 @@ in writeDash wrapper-name ''
wrapper_name=${shell.escape wrapper-name} wrapper_name=${shell.escape wrapper-name}
# TODO
for i in "$@"; do
case $i in
-h|--help)
${concatStringsSep "\n" (mapAttrsToList (name: opt: /* sh */ ''
printf ' %-16s %s\n' \
--${shell.escape opt.long} \
${shell.escape (opt.description or "undocumented flag")}
'') opts)}
exit
esac
done
${concatStringsSep "\n" (mapAttrsToList (name: opt: /* sh */ '' ${concatStringsSep "\n" (mapAttrsToList (name: opt: /* sh */ ''
unset ${opt.varname} unset ${opt.varname}
'') opts)} '') opts)}
@ -62,7 +50,11 @@ in writeDash wrapper-name ''
(filter (opt: opt.long != null) (filter (opt: opt.long != null)
(attrValues opts)))} \ (attrValues opts)))} \
-n "$wrapper_name" \ -n "$wrapper_name" \
-o "" \ -o ${shell.escape
(concatMapStringsSep ""
(opt: opt.short + optionalString (!opt.switch) ":")
(filter (opt: opt.short != null)
(attrValues opts)))} \
-s sh \ -s sh \
-- "$@") -- "$@")
if \test $? != 0; then exit 1; fi if \test $? != 0; then exit 1; fi
@ -71,7 +63,10 @@ in writeDash wrapper-name ''
while :; do while :; do
case $1 in case $1 in
${concatStringsSep "\n" (mapAttrsToList (name: opt: /* sh */ '' ${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 */ '' ${if opt.switch then /* sh */ ''
${opt.varname}=true ${opt.varname}=true
shift shift
@ -81,7 +76,7 @@ in writeDash wrapper-name ''
''} ''}
;; ;;
'') (filterAttrs '') (filterAttrs
(_: opt: opt.long != null) (_: opt: opt.long != null || opt.short != null)
opts))} opts))}
--) --)
shift shift