Skip to content
Myr

context.md

This file

Path

docs/reference/context.md

39 lines · 1005 bytes · Markdown

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 5a5206c point at it.

⬆ Bump prek from 0.3.10 to 0.3.11 (#1723)dependabot[bot]before adoption5a5206c

lines 1–39why, one step back
1# Context
23Every 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