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

11 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 18:26 +0000

1from typing import Optional 1abcdefgh

2 

3import typer 1abcdefgh

4 

5 

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

7 if ctx.resilient_parsing: 1abcdefgh

8 return 1abcdefgh

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

10 if value != "Camila": 1abcdefgh

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

12 return value 1abcdefgh

13 

14 

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

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

17 

18 

19if __name__ == "__main__": 1abcdefgh

20 typer.run(main) 1abcdefgh