Multiple options with multiple values¶
For when simple doesn't quite cut it, you may also declare a CLI option that takes several values of different types and can be used multiple times.
For this, we use the standard Python list and declare it as a list of tuple:
from typing import Annotated
import typer
app = typer.Typer()
@app.command()
def main(borrow: Annotated[list[tuple[float, str]], typer.Option()] = []):
if not borrow:
print("Congratulations, you're debt-free!")
raise typer.Exit(0)
total = 0.0
for amount, person in borrow:
print(f"Borrowed {amount:.2f} from {person}")
total += amount
print()
print(f"Total borrowed: {total:.2f}")
if __name__ == "__main__":
app()
🤓 Other versions and variants
Tip
Prefer to use the Annotated version if possible.
import typer
app = typer.Typer()
@app.command()
def main(borrow: list[tuple[float, str]] = typer.Option([])):
if not borrow:
print("Congratulations, you're debt-free!")
raise typer.Exit(0)
total = 0.0
for amount, person in borrow:
print(f"Borrowed {amount:.2f} from {person}")
total += amount
print()
print(f"Total borrowed: {total:.2f}")
if __name__ == "__main__":
app()
Just as before, the types internal to the tuple define the type of each value in the tuple.
Check it¶
$ python main.py
Congratulations, you're debt-free!
// Now let's borrow some money.
$ python main.py --borrow 2.5 Mark
Borrowed 2.50 from Mark
Total borrowed: 2.50
// And, of course, it may be used multiple times
$ python main.py --borrow 2.5 Mark --borrow 5.25 Sean --borrow 1.75 Wade
Borrowed 2.50 from Mark
Borrowed 5.25 from Sean
Borrowed 1.75 from Wade
Total borrowed: 9.50