Skip to content

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

def main(files: List[Path], celebration: str):
    for path in files:
        if path.is_file():
            print(f"This file exists: {}")

if __name__ == "__main__":

And then you can pass it as many CLI arguments of that type as you want:

$ python ./ ./ woohoo!

This file exists:
This file exists:


We also declared a final CLI argument celebration, and it's correctly used even if we pass an arbitrary number of files first.


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

def main(
    names: Tuple[str, str, str] = typer.Argument(
        ("Harry", "Hermione", "Ron"), help="Select 3 characters to play with"
    for name in names:
        print(f"Hello {name}")

if __name__ == "__main__":

Check it:

// Check the help
$ python --help

Usage: [OPTIONS] [NAMES]...

  [NAMES]...  Select 3 characters to play with  [default: Harry, Hermione, Ron]

  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it or customize the installation.
  --help                Show this message and exit.

// Use it with its defaults
$ python

Hello Harry
Hello Hermione
Hello Ron

// If you pass an invalid number of arguments you will get an error
$ python Draco Hagrid

Error: argument names takes 3 values

// And if you pass the exact number of values it will work correctly
$ python Draco Hagrid Dobby

Hello Draco
Hello Hagrid
Hello Dobby
You can ask questions about Typer. Try:
How can I terminate a program?
How to launch applications?
How to add help to CLI argument?