Skip to content

Home

shtab

Tests Coverage Quality PyPI Conda Downloads LICENCE

  • 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: shtab processes an argparse.ArgumentParser object to generate a tab completion script for your shell

Features#

  • Outputs tab completion scripts for multiple shells
    • bash, zsh, fish, tcsh
  • Supports
  • <arguments>, --options and sub 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

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#

Not working?

  • Ensure that shtab and the application you're trying to complete are both accessible from your environment.
  • Ensure that prog is set:
    • if using options.entry_points.console_scripts=MY_PROG=..., then ensure the main parser's prog matches argparse.ArgumentParser(prog="MY_PROG") or override it using shtab MY_PROG.get_main_parser --prog=MY_PROG.
    • if executing a script file ./MY_PROG.py (with a shebang #!/usr/bin/env python) directly, then use argparse.ArgumentParser(prog="MY_PROG.py") or override it using shtab MY_PROG.get_main_parser --prog=MY_PROG.py.
  • Ensure that all arguments have help messages (parser.add_argument('positional', help="documented; i.e. not hidden")).
  • Path completion is disabled by default, and must be enabled explicitly (parser.add_argument('positional').complete = shtab.FILE).
  • Ask a general question on StackOverflow.
  • Report bugs and open feature requests on GitHub.

Alternatives#

  • argcomplete
    • executes the underlying script every time <TAB> is pressed (slow and has side-effects)
  • pyzshcomplete
    • executes the underlying script every time <TAB> is pressed (slow and has side-effects)
    • only provides zsh completion
  • click
    • different framework completely replacing the builtin argparse
    • solves multiple problems (rather than POSIX-style "do one thing well")

Contributions#

Please do open issues & pull requests!

See CONTRIBUTING.md for more guidance.

git-fame

Hits