Skip to content
Advertisement

Argparse: Required argument ‘y’ if ‘x’ is present

I have a requirement as follows:

JavaScript

for the argument prox , I use action=’store_true’ to check if it is present or not. I do not require any of the arguments. But, if –prox is set I require rport and lport as well. Is there an easy way of doing this with argparse without writing custom conditional coding.

More Code:

JavaScript

Advertisement

Answer

No, there isn’t any option in argparse to make mutually inclusive sets of options.

The simplest way to deal with this would be:

JavaScript

Actually there’s already an open PR with an enhancement proposal : https://github.com/python/cpython/issues/55797

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement