How to pass a list as an argument to typer

266 Views Asked by At

main.py

import typer
from typing import List

def typer_main(
    channels: List[int] = typer.Argument([1, 2, 3], help='List of channels'),
):
    for channel in channels:
        print(channel)

Run this code

python main.py

But i get thos error

TypeError: 'default' is not supported for nargs=-1.

1

There are 1 best solutions below

0
Nikolaj Š. On

It's unsupported by Typer:

If you want a specific number of values and types, you can use a tuple, and it can even have default values

(emphasis mine)

Source: CLI Arguments with Multiple Values - Typer

There's also an Github issue confirming the problem with moderately weird workarounds posted: Default value for optional multi-value argument · Issue #518