Skip to content
Myr

context.md

This file

Path

docs/reference/context.md

39 lines · 1005 bytes · Markdown

Last changed by

📝 Remove documentation pages that reference using Click directly (#1538)Sofie Van Landeghembefore adoption2f265f2

History of this file

Take it

The walk stopped after 200 commits: lines older than 56d81f0 point at it.

📝 Update release notesgithub-actions[bot]before adoption56d81f0

lines 1–2why, one step back
1# Context
2

📝 Remove documentation pages that reference using Click directly (#1538)Sofie Van Landeghembefore adoption2f265f2

lines 3–5why, one step back
3Every 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`.

📝 Update release notesgithub-actions[bot]before adoption56d81f0

line 6why, one step back
6

📝 Remove documentation pages that reference using Click directly (#1538)Sofie Van Landeghembefore adoption2f265f2

lines 7–9why, one step back
7Similarly, you can also declare a function parameter with type `typer.CallbackParam` in case a callback could be used
8by several CLI parameters, and you need to figure out which one it was.9

📝 Update release notesgithub-actions[bot]before adoption56d81f0

lines 10–39why, one step back
10
11```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