shtab#
FILE#
DIRECTORY#
glob#
def glob(*patterns: str) -> CompleteType
Example: glob("*.yml", "*.yaml")
Consider native shell alternatives in special cases:
- any file:
shtab.FILE(instead ofglob("*")) - any directory:
shtab.DIRECTORY(instead ofglob("*/"))
cmd#
def cmd(command: str) -> CompleteType
- command: shell command to run to generate completions
Example: cmd("git branch")
complete#
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 usingparser.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#
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) thenparseris assumed to actually be a subparser (subcommand mode) - parent: required in subcommand mode
- preamble: see
completefor details
shtab.click#
add_command_to#
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#
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(...):