perf: replace a series of if statements with a case block. Add error

handling for case where unknown option is provided.

- Error handling involves writing error message to stderr, and
  forcefully exiting the init script, forcing user to fix their setup.
This commit is contained in:
Christian Fredrik Johnsen 2024-12-11 01:59:07 +01:00
parent 4ff5dbfe9b
commit 4ea68e3ca0

View File

@ -22,35 +22,37 @@ fi
mode="help" mode="help"
no_rehash="" no_rehash=""
no_push_path="" no_push_path=""
for args in "$@" shell=""
do
if [ "$args" = "-" ]; then
mode="print"
shift
fi
if [ "$args" = "--path" ]; then for arg in "$@"; do
mode="path" case "$arg" in
shift -)
fi mode="print"
;;
if [ "$args" = "--detect-shell" ]; then zsh|bash|ksh|fish)
mode="detect-shell" shell="$arg"
shift ;;
fi --path)
mode="path"
if [ "$args" = "--no-push-path" ]; then ;;
no_push_path=1 --detect-shell)
shift mode="detect-shell"
fi ;;
--no-push-path)
if [ "$args" = "--no-rehash" ]; then no_push_path=1
no_rehash=1 ;;
shift --no-rehash)
fi no_rehash=1
;;
*)
echo "Warning: Unknown option '$arg'" >&2
echo "Run 'pyenv init --help' for usage." >&2
exit 1
;;
esac
done done
shell="$1" # If shell is not provided, detect it.
if [ -z "$shell" ]; then if [ -z "$shell" ]; then
shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)" shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)"
shell="${shell%% *}" shell="${shell%% *}"