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

17 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

5from typing_extensions import Annotated 1iabcdefgh

6 

7valid_completion_items = [ 1abcdefgh

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

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

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

11] 

12 

13err_console = Console(stderr=True) 1iabcdefgh

14 

15 

16def complete_name(ctx: typer.Context, args: List[str], incomplete: str): 1iabcdefgh

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

18 names = ctx.params.get("name") or [] 1iabcdefgh

19 for name, help_text in valid_completion_items: 1iabcdefgh

20 if name.startswith(incomplete) and name not in names: 1iabcdefgh

21 yield (name, help_text) 1iabcdefgh

22 

23 

24app = typer.Typer() 1iabcdefgh

25 

26 

27@app.command() 1iabcdefgh

28def main( 1abcdefgh

29 name: Annotated[ 

30 List[str], 

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

32 ] = ["World"], 

33): 

34 for n in name: 1iabcdefgh

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

36 

37 

38if __name__ == "__main__": 1iabcdefgh

39 app() 1iabcdefgh