Here's the minimal code for what I want to do,
import argparse
parser = argparse.ArgumentParser( prog="test")
subparsers = parser.add_subparsers(title='sub-commands')
num = subparsers.add_parser("num")
num.add_argument("-n")
numr = num.add_argument_group("Required arguments")
onr =numr.add_mutually_exclusive_group(required=True)
onr.add_argument("-x")
onr2 = onr.add_argument_group("new")
onr2.add_argument("-y")
onr2.add_argument("-z")
So what I want is that, the user must provide either x
or both y
,z
, so thought of adding a mutually exclusive group of one argument and one group. And this must be under the sub command num
. This code gives this output
usage: test num [-h] [-n N] -x X [-y Y] [-z Z]
optional arguments:
-h, --help show this help message and exit
-n N
Required arguments:
-x X
No info about y
,z
, also giving both y
,z
doesn't work
python test.py num -y 9 -z 10
usage: test num [-h] [-n N] -x X [-y Y] [-z Z]
test num: error: one of the arguments -x is required
How do I achieve this using argparse, or is it even possible?