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