GitHub

command_spec

API reference for command_spec.

A command path as data, for documentation tools (Effect's CommandSpec).

resolve(app, ["app", "db", "push"]) gives the Spec of push: its description, examples, the flags it accepts (local, inherited and global, in the order help lists them, hidden ones included), its arguments and its subcommands (unlisted ones included). It reads the tree only: no handler, prompt, Config or effectful default runs.

Re-exports

Types

TypeDefinitionDescription
PlainTypeTagtype PlainTypeTag = Literal['Boolean', 'String', 'Int', 'Finite', 'Date', 'Redacted', 'FileText', 'FileParse', 'FileSchema', 'KeyValuePair', 'Never']
Typetype Type = PlainType | ChoiceType | PathValueType

What a flag or argument takes, without collapsing kinds as help's type name does.

Scopetype Scope = Local | Inherited | Global

Classes

Functions

resolve

resolve(command, path, *, built_ins=None) -> Spec | None

The Spec of the command at path (canonical names, the root's first), or None when no command is there. built_ins are the built-in global flags to report (global_flag.built_ins by default; pass CliConfig's when it differs).

ParameterTypeDescription
commandCommand[Any, Any, Any, Any]
pathSequence[str]
built_insSequence[GlobalFlag] | None = None

Returns Spec | None

def resolve(
    command: Command[Any, Any, Any, Any],
    path: Sequence[str],
    *,
    built_ins: Sequence[GlobalFlag] | None = None,
) -> Spec | None:
    """The ``Spec`` of the command at ``path`` (canonical names, the root's first), or
    ``None`` when no command is there. ``built_ins`` are the built-in global flags to
    report (``global_flag.built_ins`` by default; pass ``CliConfig``'s when it differs)."""
    path = tuple(path)
    lineage = _lineage(command, path)
    if lineage is None:
        return None
    current = lineage[-1]
    state = current.state
    flags: list[Flag] = []
    seen: set[str] = set()

    def add_flags(params: Sequence[p.Param[Any]], scope: Scope) -> None:
        for param in params:
            for entry in _entries(param):
                if entry.single.kind != p.FLAG:
                    continue
                # As in help, a nearer scope's name wins over an inherited one; globals
                # are deduplicated by identity only, since their aliases still route.
                if isinstance(scope, Inherited) and entry.single.name in seen:
                    continue
                seen.add(entry.single.name)
                flags.append(_flag(entry, scope))

    add_flags(state.config.flags, Local())
    for index, ancestor in enumerate(lineage[:-1]):
        add_flags(ancestor.state.context_config.flags, Inherited(path[: index + 1]))
    seen_globals: list[GlobalFlag] = []

    def add_globals(globals_: Sequence[GlobalFlag], declared_by: tuple[str, ...] | None) -> None:
        for global_flag in globals_:
            if any(global_flag is known for known in seen_globals):
                continue
            seen_globals.append(global_flag)
            kind: Literal["Action", "Setting"] = (
                "Action" if isinstance(global_flag, Action) else "Setting"
            )
            add_flags([global_flag.flag.param], Global(declared_by, kind))

    add_globals(global_flag_.built_ins if built_ins is None else built_ins, None)
    for index, ancestor in enumerate(lineage):
        add_globals(ancestor.state.global_flags, path[: index + 1])
    arguments = [
        _argument(entry)
        for param in state.config.arguments
        for entry in _entries(param)
        if entry.single.kind == p.ARGUMENT
    ]
    subcommands = [
        Subcommand(
            sub.name, sub.alias, group.group, sub.unlisted, sub.short_description, sub.description
        )
        for group in state.subcommands
        for sub in group.commands
    ]
    return Spec(
        path=path,
        name=state.name,
        alias=state.alias,
        description=state.description,
        short_description=state.short_description,
        unlisted=state.unlisted,
        examples=state.resolve_examples(path),
        annotations=state.annotations,
        flags=tuple(flags),
        arguments=tuple(arguments),
        subcommands=tuple(subcommands),
    )