I'm trying to figure out how to use argparser to do the following:
$ python test.py executeBuild --name foobar1 executeBuild --name foobar2 ....
getBuild
itself is a sub-command. My goal is to have the script have the capability to chain a series of sub-command (executeBuild
being one of them) and execute them in order. In the example above, it would execute a build, then setup the environment, then execute build again. How can I accomplish this with argparse? I've tried the following:
main_parser = argparse.ArgumentParser(description='main commands')
subparsers = main_parser.add_subparsers(help='SubCommands', dest='command')
build_parser = subparsers.add_parser('executeBuild')
build_parser.add_argument('--name', action='store', nargs=1, dest='build_name')
check_parser = subparsers.add_parser('setupEnv')
args, extra=main_parser.parse_known_args()
However, it appears that whenever I do this, it goes into the subcommand of executeBuild
and report it doesn't know what executeBuild
is. I've tried parsing out the extra so I can do a repeat call / chain, however, the first view property appears to have been overwritten, so I can't even just save the extra options and iterate thru.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…