Skip to content
Myr

typing_aliased_group.py

This file

Path

tests/typing/typing_aliased_group.py

46 lines · 1.2 KiB · Python

Last changed by

Not known.

History of this file

Take it

1"""Example from https://click.palletsprojects.com/en/stable/advanced/#command-aliases"""23from __future__ import annotations45from typing_extensions import assert_type67import click8910class AliasedGroup(click.Group):11    def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None:12        rv = click.Group.get_command(self, ctx, cmd_name)13        if rv is not None:14            return rv15        matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)]16        if not matches:17            return None18        elif len(matches) == 1:19            return click.Group.get_command(self, ctx, matches[0])20        ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")2122    def resolve_command(23        self, ctx: click.Context, args: list[str]24    ) -> tuple[str | None, click.Command, list[str]]:25        # always return the full command name26        _, cmd, args = super().resolve_command(ctx, args)27        assert cmd is not None28        return cmd.name, cmd, args293031@click.command(cls=AliasedGroup)32def cli() -> None:33    pass343536assert_type(cli, AliasedGroup)373839@cli.command()40def push() -> None:41    pass424344@cli.command()45def pop() -> None:46    pass