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

15 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-26 21:46 +0000

1import typer 1abcdefg

2from rich.console import Console 1abcdefg

3 

4valid_completion_items = [ 1abcdefg

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) 1abcdefg

11 

12 

13def complete_name(ctx: typer.Context, args: list[str], incomplete: str): 1abcdefg

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

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

16 for name, help_text in valid_completion_items: 1abcdefg

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

18 yield (name, help_text) 1abcdefg

19 

20 

21app = typer.Typer() 1abcdefg

22 

23 

24@app.command() 1abcdefg

25def main( 1abcdefg

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: 1abcdefg

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

32 

33 

34if __name__ == "__main__": 1abcdefg

35 app() 1abcdefg