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

13 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-19 20:29 +0000

1from typing import List 1abcdefghi

2 

3import typer 1abcdefghi

4 

5valid_completion_items = [ 1abcdefghi

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

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

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

9] 

10 

11 

12def complete_name(ctx: typer.Context, incomplete: str): 1abcdefghi

13 names = ctx.params.get("name") or [] 1abcdefghi

14 for name, help_text in valid_completion_items: 1abcdefghi

15 if name.startswith(incomplete) and name not in names: 1abcdefghi

16 yield (name, help_text) 1abcdefghi

17 

18 

19app = typer.Typer() 1abcdefghi

20 

21 

22@app.command() 1abcdefghi

23def main( 1abcdefghi

24 name: List[str] = typer.Option( 

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

26 ), 

27): 

28 for n in name: 1abcdefghi

29 print(f"Hello {n}") 1abcdefghi

30 

31 

32if __name__ == "__main__": 1abcdefghi

33 app() 1abcdefghi