[Python] Python argparse: Is there a way to specify a range in nargs?

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    I have an optional argument that supports a list of arguments itself.

    I mean, it should support:

    • -f 1 2
    • -f 1 2 3

    but not:

    • -f 1
    • -f 1 2 3 4

    Is there a way to force this within argparse ? Now I'm using nargs="*", and then checking the list length.

    Edit: As requested, what I needed is being able to define a range of acceptable number of arguments. I mean, saying (in the example) 2 or 3 args is right, but not 1 or 4 or anything that's not inside the range 2..3

    Continue reading...

Compartilhe esta Página