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

15 statements  

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

1from typing import List 1iabcdefgh

2 

3import typer 1iabcdefgh

4from rich.console import Console 1iabcdefgh

5 

6valid_completion_items = [ 1abcdefgh

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

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

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

10] 

11 

12err_console = Console(stderr=True) 1iabcdefgh

13 

14 

15def complete_name(args: List[str], incomplete: str): 1iabcdefgh

16 err_console.print(f"{args}") 1iabcdefgh

17 for name, help_text in valid_completion_items: 1iabcdefgh

18 if name.startswith(incomplete): 1iabcdefgh

19 yield (name, help_text) 1iabcdefgh

20 

21 

22app = typer.Typer() 1iabcdefgh

23 

24 

25@app.command() 1iabcdefgh

26def main( 1abcdefgh

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

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

29 ), 

30): 

31 for n in name: 1iabcdefgh

32 print(f"Hello {n}") 1iabcdefgh

33 

34 

35if __name__ == "__main__": 1iabcdefgh

36 app() 1iabcdefgh