[Python] How to require arguments in argparse and make them non-positional?

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

  1. Stack

    Stack Membro Participativo

    I have a Python script where I would like to force all required arguments to be named (non-positional).

    So the following should work:

    python myscript --arg1 arg1 --arg2 arg2
    python myscript --arg2 arg2 --arg1 arg1


    But the following should fail:

    python myscript arg1 arg2
    python myscript --madeUpArg arg1 --arg2 arg2


    From what I can tell, in argparse if you make something required, it becomes a positional argument, so both of the second examples that I want to fail will succeed (even the second case, with the made up argument!). How do I get desired functionality?

    Continue reading...

Compartilhe esta Página