Skip to content

How to have a single option that allows multiple values without repeating the option #1251

Answered by kinuax
Timmmm asked this question in Questions
Discussion options

You must be logged in to vote

Typer only supports a fixed number of values for list CLI option. Source: https://typer.tiangolo.com/tutorial/multiple-values/options-with-multiple-values/

If you want to keep --user only once, you may pass a random number of users separating them with anything but a space.

from typing_extensions import Annotated

import typer

separator = ","  # May be "|", "#", "-", etc.
assert separator not in ["", " "]


def str_to_list(value: str) -> list[str]:
    return value.split(separator)


def main(user: Annotated[list | None, typer.Option(parser=str_to_list)] = None):
    if not user:
        print(f"No provided users (raw input = {user})")
        raise typer.Abort()
    for u in user:
     …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Timmmm
Comment options

Answer selected by Timmmm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
2 participants