Skip to content

cli_app.utils.output

cli_app.utils.output

OutputFormat

Bases: StrEnum

Output format for CLI commands.

echo_json

echo_json(data)

Serialise data to JSON and write it to stdout.

Source code in src/cli_app/utils/output.py
def echo_json(data: dict[str, Any] | list[Any]) -> None:
    """Serialise *data* to JSON and write it to stdout."""
    typer.echo(json.dumps(data, indent=2, default=str))

render_output

render_output(data, fmt, *, text_render=None)

Render data in the requested format.

When fmt is OutputFormat.json the data is serialised to JSON. When fmt is OutputFormat.text text_render is called if provided; otherwise the data is printed via the Rich console.

Source code in src/cli_app/utils/output.py
def render_output(
    data: dict[str, Any] | list[Any],
    fmt: OutputFormat,
    *,
    text_render: Callable[[], None] | None = None,
) -> None:
    """Render *data* in the requested format.

    When *fmt* is ``OutputFormat.json`` the data is serialised to JSON.
    When *fmt* is ``OutputFormat.text`` *text_render* is called if provided;
    otherwise the data is printed via the Rich console.
    """
    if fmt == OutputFormat.json:
        echo_json(data)
    elif text_render is not None:
        text_render()
    else:
        get_console().print(data)