Skip to content

synth_cleo

Class info

🛈 DocStrings

Module for generating Cleo CLI implementations from CommandInfo objects.

create_app

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

Create a Cleo Application from a CommandInfo object.

Nested subcommands are registered with space-separated names ("group sub").

Parameters:

Name Type Description Default
cmd_info CommandInfo

The CommandInfo object to convert

required
handler Handler | None

Called with the invoked command's info and its parsed values. An integer return value is used as exit code.

None

Returns:

Type Description
Application

A Cleo Application with all commands and subcommands

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

    Nested subcommands are registered with space-separated names (`"group sub"`).

    Args:
        cmd_info: The CommandInfo object to convert
        handler: Called with the invoked command's info and its parsed values.
                 An integer return value is used as exit code.

    Returns:
        A Cleo Application with all commands and subcommands
    """
    app = Application(name=cmd_info.name, version="1.0.0")

    # Create default command if main command has parameters
    if any(p.name not in _APP_OPTIONS for p in cmd_info.params):
        app.add(_create_command_class(cmd_info, handler=handler)())

    # Add all subcommands recursively
    def add_commands(commands: dict[str, CommandInfo], prefix: str = "") -> None:
        for name, subcmd in commands.items():
            full_name = f"{prefix}{name}"
            app.add(_create_command_class(subcmd, name=full_name, handler=handler)())
            if subcmd.subcommands:
                add_commands(subcmd.subcommands, prefix=f"{full_name} ")

    add_commands(cmd_info.subcommands)
    return app