We also declared a final CLI argumentcelebration, and it's correctly used even if we pass an arbitrary number of files first.
Info
A List can only be used in the last command (if there are subcommands), as this will take anything to the right and assume it's part of the expected CLI arguments.
If you want a specific number of values and types, you can use a tuple, and it can even have default values:
fromtypingimportTupleimporttyperfromtyping_extensionsimportAnnotateddefmain(names:Annotated[Tuple[str,str,str],typer.Argument(help="Select 3 characters to play with")]=("Harry","Hermione","Ron"),):fornameinnames:print(f"Hello {name}")if__name__=="__main__":typer.run(main)
Tip
Prefer to use the Annotated version if possible.
fromtypingimportTupleimporttyperdefmain(names:Tuple[str,str,str]=typer.Argument(("Harry","Hermione","Ron"),help="Select 3 characters to play with"),):fornameinnames:print(f"Hello {name}")if__name__=="__main__":typer.run(main)
Check it:
fast →💬 Check the helppython main.py --help Usage: main.py [OPTIONS] [NAMES]...
Arguments: [NAMES]... Select 3 characters to play with [default: Harry, Hermione, Ron]
Options: --help Show this message and exit.
💬 Use it with its defaultspython main.py Hello Harry Hello Hermione Hello Ron
💬 If you pass an invalid number of arguments you will get an errorpython main.py Draco Hagrid Error: Argument 'names' takes 3 values
💬 And if you pass the exact number of values it will work correctlypython main.py Draco Hagrid Dobby Hello Draco Hello Hagrid Hello Dobby