Skip to content

feat: add shell completion support - #42

Merged
bhirsz merged 2 commits into
mainfrom
feat/shell-completion
Sep 1, 2026
Merged

feat: add shell completion support#42
bhirsz merged 2 commits into
mainfrom
feat/shell-completion

Conversation

@bhirsz

@bhirsz bhirsz commented Sep 1, 2026

Copy link
Copy Markdown
Member

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 ArgConfig gains two builtin options (both config=False):

  • --show-completion [SHELL] — print the completion script to stdout
  • --install-completion [SHELL] — append it to the shell's startup file

How it works

A small shell stub re-invokes the program with _<PROG>_COMPLETE=<shell>_complete plus COMP_WORDS/COMP_CWORD. The processor answers by printing type,value[,help] records (plain/file/dir) computed from the declared options — including Literal[...] choices — then exits cleanly.

Exit decoupling (bug fix)

Exit is a clean-exit control signal (raised after completion prints its output), not an error. Previously it subclassed ArgConfigError, so a host application's broad except ArgConfigError swallowed it and reported a spurious error. Exit now inherits directly from Exception.

Migration (pre-1.0): if you caught confargs.Exit via except ArgConfigError, catch Exit explicitly instead.

Tests

  • New tests/test_completion.py (27 tests) covering script rendering, request handling, record formats per shell, and prog/var derivation.
  • New regression test that Exit is not an ArgConfigError.
  • Full suite: 361 passed; mypy --strict clean; ruff lint + format clean.

Docs

  • README "Shell completion" section; CHANGELOG [Unreleased] (Features + Bug Fixes with migration note); completion usage added to both living demos.

bhirsz and others added 2 commits September 1, 2026 10:58
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>
@bhirsz
bhirsz merged commit 9a90ce8 into main Sep 1, 2026
8 checks passed
@bhirsz
bhirsz deleted the feat/shell-completion branch September 1, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant