I have a script with two primary functions, upgrade and provision. I'm using subparsers as a way to dictate the action being performed by the script but I want to avoid them being used together.
This is a snippet of the code:
import argparse
def main():
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(help='sub-command help')
parser.add_argument('--user', '-u', help='User', default=None, required=True)
parser.add_argument('--password', '-p', help='Password', default=None, required=True)
parser.add_argument('--server', '-s', help='server with Admin functionality', default=None, required=True)
subparser_prov = subparser.add_parser('provision', help='Provision new managers')
subparser_prov.set_defaults(which='provision')
subparser_upgr = subparser.add_parser('upgrade', help='Upgrade worker by replacing them')
subparser_upgr.set_defaults(which='upgrade')
subparser_upgr.add_argument('--version', help='Deployment version', default=None)
args = vars(parser.parse_args())
print args['user']
print args['password']
if args['which'] == 'provision':
print 'Provisioning new environment!'
elif args['which'] == 'upgrade':
print 'Upgrading workers! %s' % args['version']
if __name__ == "__main__":
main()
I know that add_mutually_exclusive_group() is supported by both parser and subparser however this is specifically for arguments. With a subparser is there any method for avoiding them being used together?