cmd2.argparse_completer

This module defines the ArgparseCompleter class which provides argparse-based tab completion to cmd2 apps. See the header of argparse_custom.py for instructions on how to use these features.

class cmd2.argparse_completer.ArgparseCompleter(parser: ArgumentParser, cmd2_app: Cmd, *, parent_tokens: Dict[str, List[str]] | None = None)

Automatic command line tab completion based on argparse parameters

complete(text: str, line: str, begidx: int, endidx: int, tokens: List[str], *, cmd_set: CommandSet | None = None) List[str]

Complete text using argparse metadata

Parameters:
  • text – the string prefix we are attempting to match (all matches must begin with it)

  • line – the current input line with leading whitespace removed

  • begidx – the beginning index of the prefix text

  • endidx – the ending index of the prefix text

  • tokens – list of argument tokens being passed to the parser

  • cmd_set – if tab completing a command, the CommandSet the command’s function belongs to, if applicable. Defaults to None.

Raises:

CompletionError for various types of tab completion errors

complete_subcommand_help(text: str, line: str, begidx: int, endidx: int, tokens: List[str]) List[str]

Supports cmd2’s help command in the completion of subcommand names :param text: the string prefix we are attempting to match (all matches must begin with it) :param line: the current input line with leading whitespace removed :param begidx: the beginning index of the prefix text :param endidx: the ending index of the prefix text :param tokens: arguments passed to command/subcommand :return: List of subcommand completions

format_help(tokens: List[str]) str

Supports cmd2’s help command in the retrieval of help text :param tokens: arguments passed to help command :return: help text of the command being queried

cmd2.argparse_completer.DEFAULT_AP_COMPLETER

alias of ArgparseCompleter

cmd2.argparse_completer.set_default_ap_completer_type(completer_type: Type[ArgparseCompleter]) None

Set the default ArgparseCompleter class for a cmd2 app.

Parameters:

completer_type – Type that is a subclass of ArgparseCompleter.