CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-types-click

Typing stubs for click - a command line interface creation kit for Python

Overview
Eval results
Files

commands-groups.mddocs/

Commands and Groups

Core decorators and classes for defining CLI commands and organizing them into hierarchical groups. Commands are the fundamental building blocks of CLI applications, while groups enable complex command structures with subcommands.

Capabilities

Command Creation

Creates individual CLI commands that can be executed directly. Commands wrap functions and provide CLI interface with automatic help generation, parameter parsing, and error handling.

def command(
    name: str | None = None,
    cls: Type[Command] | None = None,
    context_settings: dict[Any, Any] | None = None,
    help: str | None = None,
    epilog: str | None = None,
    short_help: str | None = None,
    options_metavar: str = "[OPTIONS]",
    add_help_option: bool = True,
    no_args_is_help: bool = False,
    hidden: bool = False,
    deprecated: bool = False,
) -> Callable[[Callable[..., Any]], Command]:
    """
    Decorator that converts a function into a CLI command.

    Parameters:
    - name: Command name (defaults to function name)
    - cls: Command class to use
    - context_settings: Context configuration
    - help: Command help text
    - epilog: Text after help
    - short_help: Brief help text
    - options_metavar: Options display format
    - add_help_option: Whether to add --help option
    - no_args_is_help: Show help when no arguments provided
    - hidden: Hide from help listing
    - deprecated: Mark as deprecated

    Returns:
    Command decorator function
    """

Usage Example:

@click.command()
@click.option('--verbose', is_flag=True, help='Enable verbose output')
def deploy(verbose):
    """Deploy the application."""
    if verbose:
        click.echo('Deploying with verbose output...')
    else:
        click.echo('Deploying...')

Group Creation

Creates command groups that can contain multiple subcommands. Groups enable building hierarchical CLI applications with organized command structures.

def group(
    name: str | None = None,
    cls: Type[Command] = Group,
    commands: dict[str, Command] | None = None,
    invoke_without_command: bool = False,
    no_args_is_help: bool | None = None,
    subcommand_metavar: str | None = None,
    chain: bool = False,
    result_callback: Callable[..., Any] | None = None,
    help: str | None = None,
    epilog: str | None = None,
    short_help: str | None = None,
    options_metavar: str = "[OPTIONS]",
    add_help_option: bool = True,
    hidden: bool = False,
    deprecated: bool = False,
    **kwargs: Any,
) -> Callable[[Callable[..., Any]], Group]:
    """
    Decorator that converts a function into a CLI command group.

    Parameters:
    - name: Group name (defaults to function name)
    - cls: Group class to use
    - commands: Initial commands dictionary
    - invoke_without_command: Allow group execution without subcommand
    - no_args_is_help: Show help when no arguments provided
    - subcommand_metavar: Subcommand display format
    - chain: Enable command chaining
    - result_callback: Callback for chained results
    - help: Group help text
    - epilog: Text after help
    - short_help: Brief help text
    - options_metavar: Options display format
    - add_help_option: Whether to add --help option
    - hidden: Hide from help listing
    - deprecated: Mark as deprecated

    Returns:
    Group decorator function
    """

Usage Example:

@click.group()
def database():
    """Database management commands."""
    pass

@database.command()
def migrate():
    """Run database migrations."""
    click.echo('Running migrations...')

@database.command()
@click.option('--force', is_flag=True, help='Force reset')
def reset(force):
    """Reset database."""
    if force:
        click.echo('Force resetting database...')
    else:
        click.echo('Resetting database...')

Command Class

Direct Command class for programmatic command creation without decorators.

class Command(BaseCommand):
    callback: Callable[..., Any] | None
    params: list[Parameter]
    help: str | None
    epilog: str | None
    short_help: str | None
    options_metavar: str
    add_help_option: bool
    no_args_is_help: bool
    hidden: bool
    deprecated: bool

    def __init__(
        self,
        name: str,
        context_settings: dict[Any, Any] | None = None,
        callback: Callable[..., Any] | None = None,
        params: list[Parameter] | None = None,
        help: str | None = None,
        epilog: str | None = None,
        short_help: str | None = None,
        options_metavar: str = "[OPTIONS]",
        add_help_option: bool = True,
        no_args_is_help: bool = False,
        hidden: bool = False,
        deprecated: bool = False,
    ) -> None:
        """
        Command class for direct instantiation.

        Parameters:
        - name: Command name
        - context_settings: Context configuration
        - callback: Function to execute
        - params: List of parameters
        - help: Command help text
        - epilog: Text after help
        - short_help: Brief help text
        - options_metavar: Options display format
        - add_help_option: Whether to add --help option
        - no_args_is_help: Show help when no arguments provided
        - hidden: Hide from help listing
        - deprecated: Mark as deprecated
        """

    def get_params(self, ctx: Context) -> list[Parameter]: ...
    def make_parser(self, ctx: Context) -> OptionParser: ...
    def invoke(self, ctx: Context) -> Any: ...

Group Class

Direct Group class for programmatic group creation with command management methods.

class Group(MultiCommand):
    commands: dict[str, Command]

    def __init__(
        self, 
        name: str | None = None, 
        commands: dict[str, Command] | None = None, 
        **attrs: Any
    ) -> None:
        """
        Group class for direct instantiation.

        Parameters:
        - name: Group name
        - commands: Initial commands dictionary
        - **attrs: Additional attributes
        """

    def add_command(self, cmd: Command, name: str | None = None) -> None:
        """
        Add a command to the group.

        Parameters:
        - cmd: Command to add
        - name: Command name (defaults to cmd.name)
        """

    def command(self, *args: Any, **kwargs: Any) -> Callable[[Callable[..., Any]], Command]:
        """
        Decorator to add a command to this group.

        Returns:
        Command decorator function
        """

    def group(self, *args: Any, **kwargs: Any) -> Callable[[Callable[..., Any]], Group]:
        """
        Decorator to add a subgroup to this group.

        Returns:
        Group decorator function
        """

    def get_command(self, ctx: Context, cmd_name: str) -> Command | None: ...
    def list_commands(self, ctx: Context) -> Iterable[str]: ...

Base Command Classes

Foundation classes providing common functionality for all command types.

class BaseCommand:
    allow_extra_args: bool
    allow_interspersed_args: bool
    ignore_unknown_options: bool
    name: str
    context_settings: dict[Any, Any]

    def __init__(self, name: str, context_settings: dict[Any, Any] | None = None) -> None: ...
    def main(self, args: list[str] | None = None, **extra: Any) -> Any: ...
    def __call__(self, *args: Any, **kwargs: Any) -> Any: ...

class MultiCommand(Command):
    no_args_is_help: bool
    invoke_without_command: bool
    subcommand_metavar: str
    chain: bool
    result_callback: Callable[..., Any]

    def resolve_command(self, ctx: Context, args: list[str]) -> tuple[str, Command, list[str]]: ...
    def get_command(self, ctx: Context, cmd_name: str) -> Command | None: ...
    def list_commands(self, ctx: Context) -> Iterable[str]: ...

Command Collection

Utility for combining multiple command sources into a single interface.

class CommandCollection(MultiCommand):
    sources: list[MultiCommand]

    def __init__(
        self, 
        name: str | None = None, 
        sources: list[MultiCommand] | None = None, 
        **attrs: Any
    ) -> None:
        """
        Collection of multiple command sources.

        Parameters:
        - name: Collection name
        - sources: List of command sources
        - **attrs: Additional attributes
        """

    def add_source(self, multi_cmd: MultiCommand) -> None:
        """
        Add a command source to the collection.

        Parameters:
        - multi_cmd: MultiCommand to add as source
        """

Install with Tessl CLI

npx tessl i tessl/pypi-types-click

docs

commands-groups.md

context-management.md

exception-handling.md

formatting.md

index.md

parameter-types.md

parameters.md

terminal-ui.md

testing-support.md

tile.json