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

14 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-14 00:18 +0000

1import typer 1iabcdefgh

2from typing_extensions import Annotated 1iabcdefgh

3 

4valid_completion_items = [ 1abcdefgh

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

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

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

8] 

9 

10 

11def complete_name(incomplete: str): 1iabcdefgh

12 completion = [] 1iabcdefgh

13 for name, help_text in valid_completion_items: 1iabcdefgh

14 if name.startswith(incomplete): 1iabcdefgh

15 completion_item = (name, help_text) 1iabcdefgh

16 completion.append(completion_item) 1iabcdefgh

17 return completion 1iabcdefgh

18 

19 

20app = typer.Typer() 1iabcdefgh

21 

22 

23@app.command() 1iabcdefgh

24def main( 1abcdefgh

25 name: Annotated[ 

26 str, typer.Option(help="The name to say hi to.", autocompletion=complete_name) 

27 ] = "World", 

28): 

29 print(f"Hello {name}") 1iabcdefgh

30 

31 

32if __name__ == "__main__": 1iabcdefgh

33 app() 1iabcdefgh