Skip to content

Multiple CLI Options

You can declare a CLI option that can be used multiple times, and then get all the values.

For example, let's say you want to accept several users in a single execution.

For this, use the standard Python typing.List to declare it as a list of str:

from typing import List, Optional

import typer_cloup as typer


def main(user: Optional[List[str]] = typer.Option(None)):
    if not user:
        typer.echo("No provided users")
        raise typer.Abort()
    for u in user:
        typer.echo(f"Processing user: {u}")


if __name__ == "__main__":
    typer.run(main)

You will receive the values as you declared them, as a list of str.

Check it:

$ python main.py

No provided users
Aborted!

// Now pass a user
$ python main.py --user Camila

Processing user: Camila

// And now try with several users
$ python main.py --user Camila --user Rick --user Morty

Processing user: Camila
Processing user: Rick
Processing user: Morty

Multiple float

The same way, you can use other types and they will be converted by Typer to their declared type:

from typing import List

import typer_cloup as typer


def main(number: List[float] = typer.Option([])):
    typer.echo(f"The sum is {sum(number)}")


if __name__ == "__main__":
    typer.run(main)

Check it:

$ python main.py

The sum is 0

// Try with some numbers
$ python main.py --number 2

The sum is 2.0

// Try with some numbers
$ python main.py --number 2 --number 3 --number 4.5

The sum is 9.5
You can ask questions about Typer. Try:
How can I terminate a program?
How to launch applications?
How to add help to CLI argument?