Last modified: Aug 12, 2025 By Alexander Williams

Python Typer Version Flag Implementation

Adding a --version flag to your Typer CLI app improves user experience. It lets users check your app's version quickly. This guide shows how to implement it properly.

Why Add a Version Flag?

A version flag is standard in CLI tools. It helps users verify which version they're running. This is crucial for debugging and support.

In Typer, we can auto-generate this flag from package metadata. No need to hardcode version numbers.

Basic Implementation

First, ensure your package has version metadata. This typically lives in pyproject.toml or __version__. Here's a simple implementation:


import typer
from importlib.metadata import version

app = typer.Typer()

def version_callback(value: bool):
    if value:
        typer.echo(f"Awesome CLI v{version('your-package-name')}")
        raise typer.Exit()

@app.callback()
def main(
    version: bool = typer.Option(
        None,
        "--version",
        callback=version_callback,
        help="Show version and exit."
    )
):
    pass

@app.command()
def hello():
    typer.echo("Hello World!")

if __name__ == "__main__":
    app()


$ python app.py --version
Awesome CLI v1.0.0

Using Package Metadata

The importlib.metadata.version() function reads your package version. Make sure your package is properly installed in development mode.

For more on packaging, see our Python Typer Packaging and Distribution Guide.

Advanced Version Handling

For more control, you can create a reusable version callback. This works well with larger Typer applications:


def get_version():
    return version('your-package-name')

def print_version():
    typer.echo(f"Awesome CLI v{get_version()}")

def version_callback(value: bool):
    if value:
        print_version()
        raise typer.Exit(code=0)

def add_version_option(app: typer.Typer):
    app.callback()(lambda version: version_callback(version))
    return app

Testing the Version Flag

Always test your version flag implementation. Typer's CliRunner makes this easy. Check our Python Typer CLI Testing with CliRunner guide.

Here's a simple test case:


from typer.testing import CliRunner

runner = CliRunner()

def test_version_flag():
    result = runner.invoke(app, ["--version"])
    assert result.exit_code == 0
    assert "Awesome CLI v" in result.stdout

Best Practices

Follow these tips for professional version flag implementation:

1. Use standard --version flag (not -v or --ver)

2. Exit immediately after showing version (don't run other code)

3. Include the package name in output

4. For exit codes, see our Python Typer Exit Codes Guide

Conclusion

Implementing a proper version flag in Typer is simple yet important. It makes your CLI app more professional and user-friendly.

Remember to keep your version in sync with package metadata. This ensures consistency across your application.

For more Typer tips, explore our other guides on advanced CLI development techniques.