Skip to content

synth_typer

Class info

🛈 DocStrings

Module for generating Typer CLI implementations from CommandInfo objects.

create_app

create_app(cmd_info: CommandInfo, handler: Handler | None = None) -> Typer

Create a Typer app from a CommandInfo object.

Parameters:

Name Type Description Default
cmd_info CommandInfo

The CommandInfo object to convert

required
handler Handler | None

Called with the invoked (sub)command's info and its parsed values. The root command is invoked before subcommands, as usual for typer.

None

Returns:

Type Description
Typer

A Typer application with all commands and subcommands

Source code in src/clinspector/synthesis/synth_typer.py
def create_app(cmd_info: CommandInfo, handler: Handler | None = None) -> typer.Typer:
    """Create a Typer app from a CommandInfo object.

    Args:
        cmd_info: The CommandInfo object to convert
        handler: Called with the invoked (sub)command's info and its parsed values.
                 The root command is invoked before subcommands, as usual for typer.

    Returns:
        A Typer application with all commands and subcommands
    """
    app = typer.Typer(
        name=cmd_info.name,
        help=cmd_info.description,
        no_args_is_help=True,
    )

    if handler or any(not is_help_param(p) for p in cmd_info.params):
        app.callback()(_create_callback(cmd_info, handler))

    # Add all subcommands recursively
    for name, subcmd in cmd_info.subcommands.items():
        if subcmd.subcommands:
            app.add_typer(create_app(subcmd, handler), name=name, hidden=subcmd.hidden)
        else:
            app.command(
                name=name,
                help=subcmd.description,
                hidden=subcmd.hidden,
                deprecated=subcmd.deprecated,
                epilog=subcmd.epilog,
            )(_create_callback(subcmd, handler))

    return app