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 for arg in "$@"; do
case "$arg" in
-)
mode="print" mode="print"
shift ;;
fi zsh|bash|ksh|fish)
shell="$arg"
if [ "$args" = "--path" ]; then ;;
--path)
mode="path" mode="path"
shift ;;
fi --detect-shell)
if [ "$args" = "--detect-shell" ]; then
mode="detect-shell" mode="detect-shell"
shift ;;
fi --no-push-path)
if [ "$args" = "--no-push-path" ]; then
no_push_path=1 no_push_path=1
shift ;;
fi --no-rehash)
if [ "$args" = "--no-rehash" ]; then
no_rehash=1 no_rehash=1
shift ;;
fi *)
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%% *}"