stockholm/krebs/5pkgs/simple/withGetopt.nix

122 lines
3.1 KiB
Nix
Raw Normal View History

2022-09-27 10:26:37 +00:00
{ coreutils, quote, stockholm, util-linux, writeDash }:
2021-10-12 17:36:09 +00:00
with stockholm.lib;
2017-09-14 22:08:47 +00:00
opt-spec: cmd-spec: let
cmd = cmd-spec opts;
cmd-script =
if typeOf cmd == "set"
then "exec ${cmd}"
else cmd;
opts = mapAttrs (name: value: value // rec {
long = value.long or (replaceStrings ["_"] ["-"] name);
ref = value.ref or "\"\$${varname}\"";
2017-09-19 18:59:08 +00:00
short = value.short or null;
2017-09-14 22:08:47 +00:00
switch = value.switch or false;
varname = value.varname or (replaceStrings ["-"] ["_"] name);
}) opt-spec;
# true if b requires a to define its default value
opts-before = a: b:
2017-11-15 15:41:29 +00:00
test ".*[$]${stringAsChars (c: "[${c}]") a.varname}([^0-9A-Za-z_].*)?" (b.default or "");
2017-09-14 22:08:47 +00:00
opts-list = let
sort-out = toposort opts-before (attrValues opts);
in
if sort-out ? result
then sort-out.result
else throw "toposort output: ${toJSON sort-out}";
wrapper-name =
if typeOf cmd == "set" && cmd ? name
then "${cmd.name}-getopt"
else "getopt-wrapper";
in writeDash wrapper-name ''
set -efu
wrapper_name=${shell.escape wrapper-name}
${concatStringsSep "\n" (mapAttrsToList (name: opt: /* sh */ ''
unset ${opt.varname}
'') opts)}
WITHGETOPT_ORIG_ARGS=$(${quote}/bin/quote "$@")
export WITHGETOPT_ORIG_ARGS
2022-09-27 10:26:37 +00:00
args=$(${util-linux}/bin/getopt \
2017-09-19 18:50:00 +00:00
-l ${shell.escape
(concatMapStringsSep ","
(opt: opt.long + optionalString (!opt.switch) ":")
(filter (opt: opt.long != null)
(attrValues opts)))} \
2017-09-19 18:51:07 +00:00
-n "$wrapper_name" \
2017-09-19 18:59:08 +00:00
-o ${shell.escape
(concatMapStringsSep ""
(opt: opt.short + optionalString (!opt.switch) ":")
(filter (opt: opt.short != null)
(attrValues opts)))} \
2017-09-14 22:08:47 +00:00
-s sh \
-- "$@")
if \test $? != 0; then exit 1; fi
eval set -- "$args"
while :; do
case $1 in
${concatStringsSep "\n" (mapAttrsToList (name: opt: /* sh */ ''
2017-09-19 18:59:08 +00:00
(${concatMapStringsSep "|" shell.escape (filter (x: x != "") [
(optionalString (opt.long != null) "--${opt.long}")
(optionalString (opt.short != null) "-${opt.short}")
])})
2017-09-14 22:08:47 +00:00
${if opt.switch then /* sh */ ''
${opt.varname}=true
shift
'' else /* sh */ ''
${opt.varname}=$2
shift 2
''}
;;
2017-09-19 18:50:00 +00:00
'') (filterAttrs
2017-09-19 18:59:08 +00:00
(_: opt: opt.long != null || opt.short != null)
2017-09-19 18:50:00 +00:00
opts))}
2017-09-14 22:08:47 +00:00
--)
shift
break
esac
done
${concatMapStringsSep "\n"
(opt: /* sh */ ''
if \test "''${${opt.varname}+1}" != 1; then
printf '%s: missing mandatory option '--%s'\n' \
"$wrapper_name" \
${shell.escape opt.long}
error=1
fi
'')
(filter
(x: ! hasAttr "default" x)
(attrValues opts))}
if test "''${error+1}" = 1; then
exit 1
fi
${concatMapStringsSep "\n"
(opt: /* sh */ ''
if \test "''${${opt.varname}+1}" != 1; then
${opt.varname}=${opt.default}
fi
'')
(filter
(hasAttr "default")
opts-list)}
${concatStringsSep "\n" (mapAttrsToList (name: opt: /* sh */ ''
export ${opt.varname}
'') opts)}
2017-09-19 18:32:46 +00:00
${cmd-script} "$@"
2017-09-14 22:08:47 +00:00
''