My requirement is to achieve following with argparse:
script.py <command> (-a|--first-name [--middle-name] [--last-name])
So basically, the script would accept one mandatory parameter , which can have different values. And then another parameter which should either be -a or --first-name. Then first-name can have further parameters.
I was exploring subparsers and mutually exclusive group, but can't figure out how to achieve this
One crude way I am handling this is
parser = argparse.ArgumentParser(prog='myscript', usage='%(prog)s <command> (-a|--first-name [--middle-name] [--last-name])')
parser.add_argument('command')
mxgroup = parser.add_mutually_exclusive_group(required=True)
mxgroup.add_argument('-a', action='store_true', help='Choose all')
mxgroup.add_argument('--first-name', dest='fname')
parser.add_argument('--middle-name', dest='mname')
parser.add_argument('--last-name', dest='lname')
args = parser.parse_args()
if (args.mname or args.lname) and not args.fname:
parser.error('middle and last names may be specified with first name only')
But I am thinking if these checks can be handled by argparse inherently then I won't have to redo it as the script grows
Any help would be much appreciated