Skip to content

shtab#

FILE#

DIRECTORY#

glob#

[view source]

def glob(*patterns: str) -> CompleteType

Example: glob("*.yml", "*.yaml")

Consider native shell alternatives in special cases:

  • any file: shtab.FILE (instead of glob("*"))
  • any directory: shtab.DIRECTORY (instead of glob("*/"))

cmd#

[view source]

def cmd(command: str) -> CompleteType
  • command: shell command to run to generate completions

Example: cmd("git branch")

complete#

[view source]

def complete(parser: ArgumentParser,
             shell: str = "bash",
             root_prefix: Opt[str] = None,
             preamble: str = "",
             choice_functions: Opt[Any] = None) -> str
  • shell: bash/zsh/tcsh/fish/powershell
  • root_prefix: prefix for shell functions to avoid clashes (default: "_{parser.prog}")
  • preamble: text to prepend to generated script (e.g. "_myprog_custom_function(){ echo hello }"). Consider using parser.add_argument().complete = shtab.cmd("echo hello") instead.
  • choice_functions: deprecated

NOTE: parser.add_argument().complete = ... can be used to define custom completions (e.g. filenames). See examples/pathcomplete.py.

add_argument_to#

[view source]

def add_argument_to(
        parser: _ActionsContainerT,
        option_string: Union[str, list[str]] = "--print-completion",
        help: str = "print shell completion script",
        parent: Opt[ArgumentParser] = None,
        preamble: Union[str, dict[str, str]] = "") -> _ActionsContainerT
  • option_string: iff positional (no - prefix) then parser is assumed to actually be a subparser (subcommand mode)
  • parent: required in subcommand mode
  • preamble: see complete for details

shtab.click#

add_command_to#

[view source]

def add_command_to(group: click.Group,
                   name='completion',
                   help=completion.__doc__)

Add completion command to 'myapp' click group:

>>> import click, shtab.click
>>> @click.group('myapp')
... def main():
...     ...
>>> shtab.click.add_command_to(main) # magic!

option#

[view source]

def option(help="Print shell completion script.", **attrs)

Attaches a --print-completion=SHELL option to 'myapp' command (to print a shell completion script and exit):

>>> import click, shtab.click
>>> @click.command('myapp')
... @shtab.click.option() # magic!
... @click.argument(...)
... @click.option(...)
... def main(...):