I have a requirement as follows:
JavaScript
x
2
1
./xyifier --prox --lport lport --rport rport
2
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
1
4
1
non_int.add_argument('--prox', action='store_true', help='Flag to turn on proxy')
2
non_int.add_argument('--lport', type=int, help='Listen Port.')
3
non_int.add_argument('--rport', type=int, help='Proxy port.')
4
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
1
3
1
if args.prox and (args.lport is None or args.rport is None):
2
parser.error("--prox requires --lport and --rport.")
3
Actually there’s already an open PR with an enhancement proposal : https://github.com/python/cpython/issues/55797