Skip to content

typer-repyt Reference

typer_repyt.build_command

Classes:

Name Description
ArgDef

Define the additional components to build a Typer Argument.

DecDef

Define a decorator function and it parameters.

OptDef

Define the additional components to build a Typer Option.

ParamDef

Define the necessary components to build a Typer Option or Argument.

Functions:

Name Description
build_command

Build a Typer command dynamically based on a function template and list of argument definitions.

ArgDef dataclass

Bases: ParamDef

Define the additional components to build a Typer Argument.

DecDef dataclass

Define a decorator function and it parameters.

OptDef dataclass

Bases: ParamDef

Define the additional components to build a Typer Option.

ParamDef dataclass

Define the necessary components to build a Typer Option or Argument.

These elements are used by both OptDef and ArgDef.

build_command

build_command(
    cli: Typer,
    func: Callable[..., None],
    /,
    *param_defs: ParamDef,
    decorators: list[DecDef] | None = None,
    include_context: bool | str = False,
)

Build a Typer command dynamically based on a function template and list of argument definitions.

Parameters:

Name Type Description Default
cli Typer

The Typer app that the command should be added to

required
func Callable[..., None]

A "template" function that will be used to build out the final function. The name of the function will be preserved as will its docstring. Though it is useful to define arguments for the function that match the opt_defs, it is not necessary. It will, however, help with static type checking.

required
param_defs ParamDef

Argument definitions that will be used to dynamically build the Typer command.

()
decorators list[DecDef] | None

An optional list of decorators to apply to the command function. Like regular decorators, they are applied in reverse order with those nearest to the function definition being called first.

None
include_context bool | str

If set, include the typer.Context as the first argument to the function. If the value is not boolean, use the passed string as the name of the context arg.

False

The following two command definitions are equivalent:

cli = typer.Typer()

@cli.command()
@simple_decorator
@complex_decorator("nitro", glycerine=True)
def static(
    t_ctx: Typer.Context,
    mite1: Annotated[str, typer.Argument(help="This is mighty argument 1")],
    dyna2: Annotated[int, typer.Option(help="This is dynamic option 2")],
    dyna1: Annotated[str, typer.Option(help="This is dynamic option 1")] = "default1",
    mite2: Annotated[int | None, typer.Argument(help="This is mighty argument 2")] = None,
):
    '''
    Just prints values of passed params
    '''
    print(f"{dyna1=}, {dyna2=}, {mite1=}, {mite2=}")


def dynamic(dyna1: str, dyna2: int, mite1: str, mite2: int | None):
    '''
    Just prints values of passed params
    '''
    print(f"{dyna1=}, {dyna2=}, {mite1=}, {mite2=}")


build_command(
    cli,
    dynamic,
    OptDef(name="dyna1", param_type=str, help="This is dynamic option 1", default="default1"),
    OptDef(name="dyna2", param_type=int, help="This is dynamic option 2"),
    ArgDef(name="mite1", param_type=str, help="This is mighty argument 1"),
    ArgDef(name="mite2", param_type=int | None, help="This is mighty argument 2", default=None),
    decorators=[
        DecDef(simple_decorator),
        DecDef(complex_decorator, dec_args=["nitro"], dec_kwargs=dict(glycerine=True)),
    ],
    include_context="t_ctx",
)