Coverage for docs_src / options_autocompletion / tutorial007_py310.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-26 21:46 +0000

1import typer 1abcdefg

2 

3valid_completion_items = [ 1abcdefg

4 ("Camila", "The reader of books."), 

5 ("Carlos", "The writer of scripts."), 

6 ("Sebastian", "The type hints guy."), 

7] 

8 

9 

10def complete_name(ctx: typer.Context, incomplete: str): 1abcdefg

11 names = ctx.params.get("name") or [] 1abcdefg

12 for name, help_text in valid_completion_items: 1abcdefg

13 if name.startswith(incomplete) and name not in names: 1abcdefg

14 yield (name, help_text) 1abcdefg

15 

16 

17app = typer.Typer() 1abcdefg

18 

19 

20@app.command() 1abcdefg

21def main( 1abcdefg

22 name: list[str] = typer.Option( 

23 ["World"], help="The name to say hi to.", autocompletion=complete_name 

24 ), 

25): 

26 for n in name: 1abcdefg

27 print(f"Hello {n}") 1abcdefg

28 

29 

30if __name__ == "__main__": 1abcdefg

31 app() 1abcdefg