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

15 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(ctx: typer.Context, args: list[str], incomplete: str): 1abcdefgh

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

15 names = ctx.params.get("name") or [] 1abcdefgh

16 for name, help_text in valid_completion_items: 1abcdefgh

17 if name.startswith(incomplete) and name not in names: 1abcdefgh

18 yield (name, help_text) 1abcdefgh

19 

20 

21app = typer.Typer() 1abcdefgh

22 

23 

24@app.command() 1abcdefgh

25def main( 1abcdefgh

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

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

28 ), 

29): 

30 for n in name: 1abcdefgh

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

32 

33 

34if __name__ == "__main__": 1abcdefgh

35 app() 1abcdefgh