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
  • 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: ArgumentParser,
                    option_string: Union[str,
                                         list[str]] = "--print-completion",
                    help: str = "print shell completion script",
                    parent: Opt[ArgumentParser] = None,
                    preamble: Union[str, dict[str, str]] = "")
  • 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