CLI Arguments with Multiple Values
CLI arguments can also receive multiple values.
You can define the type of a CLI argument using typing.List
.
from pathlib import Path
from typing import List
import typer_cloup as typer
def main(files: List[Path], celebration: str):
for path in files:
if path.is_file():
typer.echo(f"This file exists: {path.name}")
typer.echo(celebration)
if __name__ == "__main__":
typer.run(main)
And then you can pass it as many CLI arguments of that type as you want:
python main.py ./index.md ./first-steps.md woohoo!
This file exists: index.md
woohoo!
This file exists: first-steps.md
woohoo!
restart β»
This file exists: index.md
woohoo!
This file exists: first-steps.md
woohoo!
restart β»
Tip
We also declared a final CLI argument celebration
, 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.
CLI arguments with tuplesΒΆ
If you want a specific number of values and types, you can use a tuple, and it can even have default values:
from typing import Tuple
import typer_cloup as typer
def main(
names: Tuple[str, str, str] = typer.Argument(
("Harry", "Hermione", "Ron"), help="Select 3 characters to play with"
)
):
for name in names:
typer.echo(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)
Check it: