1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-03-06 08:47:00 +00:00
nix-darwin/pkgs/nix-tools/darwin-option.sh
2019-05-01 13:26:04 +02:00

77 lines
1.5 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#! @shell@
set -e
set -o pipefail
export PATH=@path@:$PATH
evalNix() {
nix-instantiate --eval --strict "${extraEvalFlags[@]}" -E "with import <darwin> {}; $*"
}
evalAttrs() {
evalNix "builtins.concatStringsSep \"\\n\" (builtins.attrNames $*)"
}
evalOpt() {
evalNix "options.$option.$*" 2>/dev/null
}
evalOptText() {
eval printf "$(evalNix "options.$option.$*" 2>/dev/null)" 2>/dev/null
echo
}
showSyntax() {
echo "$0: [-I path] <option>" >&2
eval printf "$(evalAttrs "options")"
echo
exit 1
}
# Parse the command line.
origArgs=("$@")
extraEvalFlags=()
option=
while [ "$#" -gt 0 ]; do
i="$1"; shift 1
case "$i" in
--help)
showSyntax
;;
-I)
if [ -z "$1" ]; then
echo "$0: $i requires an argument"
exit 1
fi
j="$1"; shift 1
extraEvalFlags+=("$i" "$j")
;;
*)
option="$i"
;;
esac
done
if [ -z "$option" ]; then showSyntax; fi
if [ "$(evalOpt "_type")" = '"option"' ]; then
echo "Value:"
evalOpt "value" || echo "no value"
echo
echo "Default:"
evalOpt "default" || evalOptText "defaultText" || echo "no default"
echo
echo "Example:"
if [ "$(evalOpt "example._type")" = '"literalExample"' ]; then
evalOptText "example.text" || echo "no example"
else
evalOpt "example" || echo "no example"
fi
echo
echo "Description:"
evalOptText "description" || echo "no description"
echo
else
eval printf "$(evalAttrs "options.$option")" 2>/dev/null
echo
fi