Coverage for docs_src / options_autocompletion / tutorial007.py: 100%
13 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
1from typing import Annotated 1abcdefghij
3import typer 1abcdefghij
5valid_completion_items = [ 1abcdefghij
6 ("Camila", "The reader of books."),
7 ("Carlos", "The writer of scripts."),
8 ("Sebastian", "The type hints guy."),
9]
12def complete_name(ctx: typer.Context, incomplete: str): 1abcdefghij
13 names = ctx.params.get("name") or [] 1abcdefghij
14 for name, help_text in valid_completion_items: 1abcdefghij
15 if name.startswith(incomplete) and name not in names: 1abcdefghij
16 yield (name, help_text) 1abcdefghij
19app = typer.Typer() 1abcdefghij
22@app.command() 1abcdefghij
23def main( 1abcdefghij
24 name: Annotated[
25 list[str],
26 typer.Option(help="The name to say hi to.", autocompletion=complete_name),
27 ] = ["World"],
28):
29 for n in name: 1abcdefghij
30 print(f"Hello {n}") 1abcdefghij
33if __name__ == "__main__": 1abcdefghij
34 app() 1abcdefghij