[Python] Using the click package: Making an option be a choice of `int`

Discussão em 'Python' iniciado por Stack, Junho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a click.option be a choice between the numbers 0, 1 and 2. I tried this:

    @click.Option('--verbosity', default=1, type=click.Choice([0, 1, 2]))
    def f():
    # ...
    pass


    But I get this exception:


    TypeError: sequence item 0: expected str instance, int found
    ​

    I guess click expects only strings in click.Choice. Any way to get it to accept ints? I know I can convert to int manually after it's received, but if there's an idiomatic way to receive an int choice, that'll be better.

    Continue reading...

Compartilhe esta Página