Home

- What: Automatically generate shell tab completion scripts for Python CLI apps
- Why: Speed & correctness. Alternatives like argcomplete and pyzshcomplete are slow and have side-effects
- How:
shtabprocesses anargparse.ArgumentParserobject to generate a tab completion script for your shell
Features#
- Outputs tab completion scripts for multiple shells
bash,zsh,fish,tcsh,powershell
- Supports
<arguments>,--optionsandsub commands- Choices (
--say={hello,goodbye}) - Paths (
--file={*.y*ml,*.toml},--dir=*/) - Dynamic shell commands (
--branch=$(git branch))
Installation#
scripts#
Tip
TL;DR where to save a completion script for a program called NAME
| shell | location |
|---|---|
bash | /etc/bash_completion.d/NAME |
zsh | /usr/local/share/zsh/site-functions/_NAME |
tcsh | /etc/profile.d/completion_NAME.csh, source in ~/.cshrc or ~/.tcshrc |
fish | ~/.config/fish/completions/NAME.fish |
powershell | ~\.config\powershell\completions\NAME.ps1, source in $PROFILE |
For more information, click on the shells above, and/or see CLI Usage.
shtab#
pip install shtab
conda install -c conda-forge shtab
bash users who have never used any kind of tab completion before should also follow the OS-specific instructions below.
Recent versions should have completion already enabled. For older versions, first run sudo apt install --reinstall bash-completion, then make sure these lines appear in ~/.bashrc:
# enable bash completion in interactive shells
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
First run brew install bash-completion, then add the following to ~/.bash_profile:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
FAQs#
- Ensure that
shtaband the application you're trying to complete are both accessible from your environment. - Ensure that
progmatches the executable name:- if using
project.scripts.MY-PROG=..., then- set the main parser's name (
argparse.ArgumentParser(prog="MY-PROG"),argopt(prog="MY-PROG"),click.group("MY-PROG"),click.command("MY-PROG"), etc) - or override it using
shtab MY_PROG.get_main_parser --prog=MY-PROG
- set the main parser's name (
- if executing a script file
./MY_PROG.py(with a shebang#!/usr/bin/env python) directly, then useprog="MY_PROG.py"
- if using
- Any suppressed argument (
help=argparse.SUPPRESS, clickhidden=True/deprecated=True) is skipped. - Default completion (when no choices are specified) is disabled. Enable it explicitly via e.g.
parser.add_argument('positional').complete = shtab.FILE. - Some shells (e.g.
zsh,fish) print information during tab completion:- subparser
descriptiontakes precedence overhelp - argument
metavartakes precedence overdest
- subparser
- Ask a general question on StackOverflow.
- Report bugs and open feature requests on GitHub.
Alternatives#
All these execute the underlying script every time <TAB> is pressed (slow and have side-effects):
- argcomplete
- pyzshcomplete (only provides
zshcompletion) - click (don't want to migrate away from
click? Useshtab's one-linerclickintegration in the CLI or in a library)
Contributions#
Please do open issues & pull requests!
See CONTRIBUTING.md for more guidance.