summaryrefslogtreecommitdiffstats
path: root/krebs
diff options
context:
space:
mode:
authorlassulus <lassulus@lassul.us>2017-09-19 20:45:15 +0200
committerlassulus <lassulus@lassul.us>2017-09-19 20:45:15 +0200
commitd30480f1457cf4ac4f67f5e02faec75765706e50 (patch)
tree3351e0b97b3ea5ac5cdfc382559e0834714a9b9a /krebs
parent358ff6da9c9b7b2ae574f759a8af6266ce642baa (diff)
parented9a1085946589409caba6e18233c329d99c0b4b (diff)
Merge remote-tracking branch 'ni/master'
Diffstat (limited to 'krebs')
-rw-r--r--krebs/5pkgs/simple/withGetopt.nix106
1 files changed, 106 insertions, 0 deletions
diff --git a/krebs/5pkgs/simple/withGetopt.nix b/krebs/5pkgs/simple/withGetopt.nix
new file mode 100644
index 000000000..fd59adcaf
--- /dev/null
+++ b/krebs/5pkgs/simple/withGetopt.nix
@@ -0,0 +1,106 @@
+with import <stockholm/lib>;
+{ utillinux, writeDash }:
+
+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}\"";
+ 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:
+ test ".*[$]${stringAsChars (c: "[${c}]") a.varname}\\>.*" (b.default or "");
+
+ 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)}
+
+ args=$(${utillinux}/bin/getopt \
+ -n "$wrapper_name" \
+ -o "" \
+ -l ${concatMapStringsSep ","
+ (opt: opt.long + optionalString (!opt.switch) ":")
+ (attrValues opts)} \
+ -s sh \
+ -- "$@")
+ if \test $? != 0; then exit 1; fi
+ eval set -- "$args"
+
+ while :; do
+ case $1 in
+ ${concatStringsSep "\n" (mapAttrsToList (name: opt: /* sh */ ''
+ --${opt.long})
+ ${if opt.switch then /* sh */ ''
+ ${opt.varname}=true
+ shift
+ '' else /* sh */ ''
+ ${opt.varname}=$2
+ shift 2
+ ''}
+ ;;
+ '') opts)}
+ --)
+ 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)}
+
+ ${cmd-script}
+''