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

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

The simplest way to deal with this would be:

if args.prox and (args.lport is None or args.rport is None):
    parser.error("--prox requires --lport and --rport.")

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

Leave a Comment