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

14 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-02-09 12:36 +0000

1import typer 1abcdefgh

2from rich.console import Console 1abcdefgh

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 

10err_console = Console(stderr=True) 1abcdefgh

11 

12 

13def complete_name(args: list[str], incomplete: str): 1abcdefgh

14 err_console.print(f"{args}") 1abcdefgh

15 for name, help_text in valid_completion_items: 1abcdefgh

16 if name.startswith(incomplete): 1abcdefgh

17 yield (name, help_text) 1abcdefgh

18 

19 

20app = typer.Typer() 1abcdefgh

21 

22 

23@app.command() 1abcdefgh

24def main( 1abcdefgh

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

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

27 ), 

28): 

29 for n in name: 1abcdefgh

30 print(f"Hello {n}") 1abcdefgh

31 

32 

33if __name__ == "__main__": 1abcdefgh

34 app() 1abcdefgh