Skip to content

introspect_click

Class info

🛈 DocStrings

get_info

get_info(instance: Command, command: str | None = None) -> CommandInfo

Return a CommandInfo object for command of given click Command / Group.

Parameters:

Name Type Description Default
instance Command

A click Command or Group instance

required
command str | None

The command to get info for (supports dot notation for subcommands).

None
Source code in src/clinspector/introspect/introspect_click.py
def get_info(
    instance: click.Command,
    command: str | None = None,
) -> commandinfo.CommandInfo:
    """Return a `CommandInfo` object for command of given click Command / Group.

    Args:
        instance: A **click** `Command` or `Group` instance
        command: The command to get info for (supports dot notation for subcommands).
    """
    return resolve_command(parse(instance), command)

parse

parse(command: Command, parent: Context | None = None) -> CommandInfo

Get a CommandInfo dataclass for given click Command.

Parameters:

Name Type Description Default
command Command

The click Command to get info for.

required
parent Context | None

The optional parent context

None
Source code in src/clinspector/introspect/introspect_click.py
def parse(
    command: click.Command,
    parent: click.Context | None = None,
) -> commandinfo.CommandInfo:
    """Get a `CommandInfo` dataclass for given click `Command`.

    Args:
        command: The **click** `Command` to get info for.
        parent: The optional parent context
    """
    ctx = click.Context(command, parent=parent)
    subcommands = getattr(command, "commands", {})
    formatter = ctx.make_formatter()
    pieces = command.collect_usage_pieces(ctx)
    formatter.write_usage(ctx.command_path, " ".join(pieces), prefix="")
    usage = formatter.getvalue().rstrip("\n")
    # Generate the full usage string based on parents if any, i.e. `root sub1 sub2 ...`.
    full_path = []
    current: click.Context | None = ctx
    while current is not None:
        name = current.command.name.lower() if current.command.name else ""
        full_path.append(name)
        current = current.parent
    full_path.reverse()
    return commandinfo.CommandInfo(
        name=command.name or "",
        description=command.help or command.short_help or "",
        usage=" ".join(full_path) + usage,
        params=[_parse_param(p) for p in command.get_params(ctx)],
        subcommands={k: parse(v, parent=ctx) for k, v in subcommands.items()},
        deprecated=bool(command.deprecated),
        epilog=command.epilog,
        hidden=command.hidden,
        callback=command.callback,
    )