Adding a Version Flag

It's common to check a CLI app's version via a --version flag.

Concretely, we want the following behavior:

$ mypackage --version
1.2.3

To achieve this in Typer, we need the following bulky implementation:

typer_app = typer.Typer()


def version_callback(value: bool):
    if not value:
        return
    print("1.2.3")
    raise typer.Exit()


@typer_app.callback()
def common(
    version: Annotated[
        bool,
        typer.Option(
            "--version",
            "-v",
            callback=version_callback,
            help="Print version.",
        ),
    ] = False,
):
    print("Callback body executed.")


print("Typer:")
typer_app(["--version"])
# 1.2.3

Not only is this a lot of boilerplate, but it also has some nasty side-effects, such as impacting whether or not you need to specify the command in a single-command program. On top of that, it's not very intuitive. Would you expect "Callback body executed." to be printed? When does version_callback get called? What is value?

With Cyclopts, the version is automatically detected by checking the version of the package instantiating App. If you prefer explicitness, version can also be explicitly supplied to App.

cyclopts_app = cyclopts.App(version="1.2.3")
cyclopts_app(["--version"])
# 1.2.3