context.md
This file
Path
docs/reference/context.md
39 lines · 1005 bytes · Markdown
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than b0f8c2d point at it.
⬆ Bump ty from 0.0.16 to 0.0.17 (#1544)dependabot[bot]before adoptionb0f8c2d
lines 1–39why, one step back1# Context23Every app has a special internal object that keeps track of state relevant to the script's execution.4For some advanced use-cases, you may want to access it directly.5This can be done by declaring a function parameter of type `typer.Context`.67Similarly, you can also declare a function parameter with type `typer.CallbackParam` in case a callback could be used8by several CLI parameters, and you need to figure out which one it was.91011```python12from typing import Annotated1314import typer1516app = typer.Typer()171819def name_callback(ctx: typer.Context, param: typer.CallbackParam, value: str):20 if ctx.resilient_parsing:21 return22 print(f"Validating param: {param.name}")23 if value != "Rick":24 raise typer.BadParameter("Only Rick is allowed")25 return value262728@app.command()29def main(name: Annotated[str | None, typer.Option(callback=name_callback)] = None):30 print(f"Hello {name}")313233if __name__ == "__main__":34 app()35```3637::: typer.Context3839::: typer.CallbackParam