[Fix] nvm: treat a bare --only like an empty --only=

Every command that accepts `--only=<categories>` only recognized it with an `=`, so a bare `--only` got whatever an unknown argument gets, and no command said what was wrong with it.
`nvm ls`, `nvm ls-remote`, `nvm exec`, `nvm alias`, and `nvm version-remote` rejected it as an unsupported option; `nvm uninstall` and `nvm reinstall-packages` printed their usage; `nvm use`, `nvm which`, `nvm run`, and `nvm version` took it, or the version after it, as the version to look for; and `nvm install --only 20` printed grep's usage, while `nvm install 20 --only` ignored it.
A bare `--only` is now parsed as `--only=` with an empty value, so every command gives the same error for both: `--only` needs a comma-separated list of categories.
Aliases already reject such a value when they are created, so stored alias targets need no change.
This commit is contained in:
Jordan Harband committed 2026-11-02 13:44:48 -06:00
1 parent ed2de88826
commit d0ebdd3709
2 files changed
+105 -27

No files matched your search

@@ -0,0 +1,65 @@
#!/bin/sh
die () { echo "$@" ; cleanup ; exit 1; }
cleanup () {
NVM_DIR="${ORIG_NVM_DIR}"
rm -rf "${TMP_DIR}"
}
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
unset NVM_ONLY NVM_ONLY_SELECTED
NVM_INDEX_NO_FETCH=1
export NVM_INDEX_NO_FETCH
nvm_download() { return 1; }
TMP_DIR="$(mktemp -d)"
ORIG_NVM_DIR="${NVM_DIR}"
NVM_DIR="${TMP_DIR}"
EXPECTED='Invalid `--only` value "": it must be a comma-separated list of any of: alpha, lts, non-lts, iojs'
# args: the flag, then nvm's arguments, with `@` where the flag goes
try_with_flag () {
FLAG="$1"
shift
ARG_COUNT=$#
for ARG in "$@"; do
if [ "${ARG}" = '@' ]; then
set -- "$@" "${FLAG}"
else
set -- "$@" "${ARG}"
fi
done
shift "${ARG_COUNT}"
COMMAND_LINE="nvm $*"
try_err nvm "$@"
}
# args: nvm's arguments, with `@` where `--only=` or `--only` goes
check () {
for FLAG in --only= --only; do
try_with_flag "${FLAG}" "$@"
[ "_${CAPTURED_EXIT_CODE}" = '_55' ] || die "${COMMAND_LINE} exited ${CAPTURED_EXIT_CODE}, expected 55"
[ "_${CAPTURED_STDERR}" = "_${EXPECTED}" ] || die "${COMMAND_LINE} printed >${CAPTURED_STDERR}<, expected >${EXPECTED}<"
done
}
check install @ 20
check install 20 @
check uninstall @ 20
check use @ 20
check run @ 20 --version
check exec @ 20 node --version
check ls @
check ls-remote @
check which @ 20
check alias foo @ 20
check reinstall-packages @ 20
check version @ 20
check version-remote @ 20
cleanup