Coverage for docs_src / options / callback / tutorial004_an_py39.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-02-09 12:36 +0000

1from typing import Annotated, Optional 1abcdefgh

2 

3import typer 1abcdefgh

4 

5app = typer.Typer() 1abcdefgh

6 

7 

8def name_callback(ctx: typer.Context, param: typer.CallbackParam, value: str): 1abcdefgh

9 if ctx.resilient_parsing: 1abcdefgh

10 return 1abcdefgh

11 print(f"Validating param: {param.name}") 1abcdefgh

12 if value != "Camila": 1abcdefgh

13 raise typer.BadParameter("Only Camila is allowed") 1abcdefgh

14 return value 1abcdefgh

15 

16 

17@app.command() 1abcdefgh

18def main(name: Annotated[Optional[str], typer.Option(callback=name_callback)] = None): 1abcdefgh

19 print(f"Hello {name}") 1abcdefgh

20 

21 

22if __name__ == "__main__": 1abcdefgh

23 app() 1abcdefgh