how can i get a –version to the root of a typertyper application?

This is addressed in the documentation:

But as those CLI parameters are handled by each of those commands, they don’t allow us to create CLI parameters for the main CLI application itself.

But we can use @app.callback() for that.

It’s very similar to @app.command(), but it declares the CLI parameters for the main CLI application (before the commands):

To do what you want, you could write something like this:

import typer
from typing import Optional

__version__ = "0.1.0"


def version_callback(value: bool):
    if value:
        typer.echo(f"Awesome CLI Version: {__version__}")
        raise typer.Exit()


app = typer.Typer(
    add_completion=False,
)


@app.callback()
def common(
    ctx: typer.Context,
    version: bool = typer.Option(None, "--version", callback=version_callback),
):
    pass


@app.command()
def main() -> None:
    pass


@app.command()
def foo() -> None:
    pass


if __name__ == "__main__":
    app()

Which gives us:

$ python typertest.py --help
Usage: typertest.py [OPTIONS] COMMAND [ARGS]...

Options:
  --version
  --help     Show this message and exit.

Commands:
  foo
  main

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top