feat: add shell completion support - #42
Merged
Merged
Conversation
Add opt-in shell completion for bash, zsh, fish and PowerShell using a self-contained, Click-style protocol (no third-party dependency). Two new builtin options are available on every `ArgConfig`: --show-completion [SHELL] print the completion script to stdout --install-completion [SHELL] append it to the shell's startup file A shell-side stub re-invokes the program with `_<PROG>_COMPLETE=<shell>_complete` and `COMP_WORDS`/`COMP_CWORD`; the processor answers the request by printing `type,value[,help]` records (plain/file/dir) computed from the declared options, including `Literal[...]` choices, then exits cleanly. Also decouple `Exit` from `ArgConfigError`: `Exit` is a clean-exit control signal (raised after completion output), not an error, so it must not be swallowed by a host application's broad `except ArgConfigError`. It now inherits directly from `Exception`. Migration (pre-1.0): if you previously caught `confargs.Exit` via `except ArgConfigError`, catch `Exit` explicitly instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add ``pwsh`` alongside ``powershell`` as a supported completion shell. On Windows the two PowerShell editions use different startup profiles: ``powershell`` (Windows PowerShell 5.1) reads ``Documents\WindowsPowerShell`` while ``pwsh`` (PowerShell 7+) reads ``Documents\PowerShell``. Installing into the wrong one silently does nothing, so ``--install-completion`` now targets the profile of the edition named. The generated script is identical except it advertises its own ``<shell>_complete`` instruction (templated), so the runtime dispatch stays consistent. A future change can auto-detect the running edition and pick the default for the user. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds opt-in shell completion for bash, zsh, fish and PowerShell, implemented with our own Click-style protocol — no third-party dependency (argcomplete/shtab are argparse-bound and don't fit confargs' own tokenizer).
Every
ArgConfiggains two builtin options (bothconfig=False):--show-completion [SHELL]— print the completion script to stdout--install-completion [SHELL]— append it to the shell's startup fileHow it works
A small shell stub re-invokes the program with
_<PROG>_COMPLETE=<shell>_completeplusCOMP_WORDS/COMP_CWORD. The processor answers by printingtype,value[,help]records (plain/file/dir) computed from the declared options — includingLiteral[...]choices — then exits cleanly.Exitdecoupling (bug fix)Exitis a clean-exit control signal (raised after completion prints its output), not an error. Previously it subclassedArgConfigError, so a host application's broadexcept ArgConfigErrorswallowed it and reported a spurious error.Exitnow inherits directly fromException.Migration (pre-1.0): if you caught
confargs.Exitviaexcept ArgConfigError, catchExitexplicitly instead.Tests
tests/test_completion.py(27 tests) covering script rendering, request handling, record formats per shell, and prog/var derivation.Exitis not anArgConfigError.mypy --strictclean; ruff lint + format clean.Docs
[Unreleased](Features + Bug Fixes with migration note); completion usage added to both living demos.