Skip to content

feat(nodeenv): print shell completion scripts with shtab - #460

Merged
ekalinin merged 4 commits into
masterfrom
feat/shell-completion
Oct 31, 2026
Merged

ekalinin merged 4 commits into
masterfrom
feat/shell-completion

Conversation

@ekalinin

@ekalinin ekalinin commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

See CONTRIBUTING.md for the rules behind this checklist.

#351 asks for shell completion, using shtab. shtab builds the script from
the argparse parser, but out of the box it leaves fish and zsh with no
candidates after -r, -C, -p and DEST_DIR, which is worse than having
no script: without one, both shells offer file names there.

nodeenv --print-completion SHELL prints a script for bash, zsh or
fish and exits:

  • shtab is optional, installed with the completion extra
    (pip install "nodeenv[completion]", shtab>=1.11), like certifi.
    Only this flag imports it, so nothing else in nodeenv needs it.
  • The flag is always in --help. Without shtab it exits with the pip
    command to run; with a shtab too old for the shell, with the same
    command and -U. No traceback in either case.
  • File names are completed after -r, --local-requirements, -C and
    --make, directory names after -p and for DEST_DIR. --node offers
    latest, lts and system (no system on Windows), --npm offers
    latest.
  • The script always completes nodeenv, also when python nodeenv.py or
    python -m nodeenv prints it.
  • The script is built on the platform it is printed on, so a Windows one
    has no Unix-only options.

PowerShell is left out for now. shtab's PowerShell script has two bugs
that showed up in CI here: -c and -C clash in its case-insensitive
hash literals (tqdm/shtab#267), and DEST_DIR is not completed after a
flag without a value, such as --force (tqdm/shtab#268). It can be added once shtab
fixes both.

tests/test_completion.py loads the printed script into bash, zsh and
fish and checks the candidates each shell offers for the same command
lines. The activate-shells job runs it with NODEENV_REQUIRE_SHELLS=1,
and the tox matrix runs it wherever the shells exist. The tests also pass
with shtab 1.11.0, the minimum in the extra.

README gets a "Shell completion" section with the install command for
each shell. In zsh the script registers from fpath or through eval
after compinit, but not when sourced.

Fixes #351

  • Tests cover the change
  • tox passes: the tests and flake8
  • CHANGES has an entry under Version [unreleased], if nodeenv's
    behavior changes
  • README.rst is updated, if the change affects what it documents
  • The title has the form type(scope): subject

--print-completion SHELL prints a completion script for bash, zsh,
fish or PowerShell, made by shtab from the same parser that reads the
command line. shtab is an optional dependency, installed with the
completion extra, and only this flag imports it: without shtab the
flag stays in --help and exits with the pip command to run.

Out of the box shtab leaves fish and zsh with no candidates after the
options that take a path, which is worse than no script at all, so the
flag marks the file and directory options. It also suggests latest,
lts and system for --node and latest for --npm. The script always
completes nodeenv, also when python -m nodeenv prints it.

Fixes #351
The activate-shells job already installs zsh and fish, and the ubuntu
image ships bash and pwsh, so it runs tests/test_completion.py with
NODEENV_REQUIRE_SHELLS=1: a missing shell fails the job instead of
skipping its tests.
pwsh refused the script: shtab keys its PowerShell tables by option
string, PowerShell hash keys ignore case, and -c (--clean-src) clashed
with -C (--config-file). The PowerShell script now leaves out -c;
--clean-src and -C still complete. The other shells get the same
script as before.
shtab's PowerShell script had a second bug after the -c workaround:
the completer switches to an option it meets and only goes back to the
positional arguments once the option has taken its values, which a
flag without a value never does. DEST_DIR was not completed after
--force, --prebuilt and the like.

--print-completion now offers bash, zsh and fish, and the -c
workaround goes away with PowerShell. Without PowerShell the completion
extra needs shtab 1.11, which passes tests/test_completion.py.
PowerShell can come back once shtab fixes both bugs
(tqdm/shtab#267 for the first one).
@ekalinin
ekalinin merged commit 0fd263f into master Oct 31, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature] Shell completion

1 participant