Skip to content

Context

When you create a Typer application it uses Click underneath. And every Click application has a special object called a Context that is normally hidden. But you can access the context by declaring a function parameter of type typer.Context.

The same way you can access the context by declaring a function parameter with its value, you can declare another function parameter with type typer.CallbackParam to get the specific Click Parameter object.

from typing import Annotated

import typer

app = typer.Typer()


def name_callback(ctx: typer.Context, param: typer.CallbackParam, value: str):
    if ctx.resilient_parsing:
        return
    print(f"Validating param: {param.name}")
    if value != "Rick":
        raise typer.BadParameter("Only Rick is allowed")
    return value


@app.command()
def main(name: Annotated[str | None, typer.Option(callback=name_callback)] = None):
    print(f"Hello {name}")


if __name__ == "__main__":
    app()

typer.Context

Bases: Context

The Context has some additional data about the current execution of your program. When declaring it in a callback function, you can access this additional information.

typer.CallbackParam

Bases: Parameter

In a callback function, you can declare a function parameter with type CallbackParam to access the specific Click Parameter object.