Skip to content

topmark.cli.main

topmark / cli / main

TopMark CLI entry point.

Defines the root Click command group and wires together all subcommands.

Key ideas: - The root Click group bootstraps a shared typed CLI state. - Subcommands own verbosity/color options and initialize shared human-output state. - The root command ensures help output has a console available.

This module intentionally stays compact to keep CLI wiring explicit and predictable.

cli

cli(ctx)

TopMark CLI entry point.

Bootstraps shared CLI state and delegates execution to subcommands. When invoked without a subcommand, prints a short hint and the help text.

Source code in src/topmark/cli/main.py
@click.group(
    cls=click.Group,
    context_settings=GROUP_CONTEXT_SETTINGS,
    invoke_without_command=True,  # Always invoke the cli() function
    help="Inspect, validate, and manage TopMark headers and configuration.",
    epilog=(
        "\b\n"
        "Examples:\n"
        "  # Validate headers in a project (dry-run)\n"
        f"  topmark {CliCmd.CHECK} src\n"
        "  # Apply header updates in place\n"
        f"  topmark {CliCmd.CHECK} --apply .\n"
        "  # Write a starter configuration file\n"
        f"  topmark {CliCmd.CONFIG} {CliCmd.CONFIG_INIT} > topmark.toml\n"
        "  # Validate the merged configuration\n"
        f"  topmark {CliCmd.CONFIG} check\n"
        "  # Inspect file types (machine-readable output)\n"
        f"  topmark {CliCmd.REGISTRY} {CliCmd.REGISTRY_FILETYPES} "
        f"{CliOpt.OUTPUT_FORMAT}={OutputFormat.JSON.value}\n"
        "  # Print the installed TopMark version\n"
        f"  topmark {CliCmd.VERSION}\n"
    ),
)
@click.pass_context
def cli(
    ctx: click.Context,
) -> None:
    """TopMark CLI entry point.

    Bootstraps shared CLI state and delegates execution to subcommands.
    When invoked without a subcommand, prints a short hint and the help text.
    """
    # Check the Python version (may exit)
    check_python_version()

    # Subcommands now own verbosity/color options and call init_common_state().
    # Ensure we still have a console for the root group help output.
    state: TopmarkCliState = bootstrap_cli_state(ctx)
    console: ConsoleProtocol = state.console

    if ctx.invoked_subcommand is None:
        console.print(ctx.get_help())