I have a set of parsers and subparsers to build a production or development system.
If the user picks production, he can add options and all is well.
If he pics development, he can enter an architecture and then enter build options.
This is where it gets sticky.
I want him to be able to select build option 'comms' 'server' or 'all', but if he picks server, he has more choices.
My implementation is below. I tried combinations of parsers and subpasers (it seems that arguments can only be added to parsers, not subparsers, is that correct?)
It falls apart for 2 reasons:
1) I can only select arch or build and I need to select both
2) If I select build it always requires that I pick 'server', even if I pick one of the other two.
So I would like something like this ./buildServer.py Dev arch -arm build -comms or ./buildServer.py Dev arch -arm build -server -tcp
I'd appreciate and help/guidance I can get - TIA
Code:
def verify():
main_parser = argparse.ArgumentParser()
main_subparsers = main_parser.add_subparsers(title="main", dest="main_command")
# parser production choices
prod_parser = main_subparsers.add_parser("prod", help="Prod")
prod_parser.add_argument("-c", "--change", action='store_true', dest="change_sig", default=False, help="Change signature information (default = %(default)s)")
prod_parser.add_argument("-sd", "--sign-deb", action='store_true', dest="sign_deb", default=False, help="Add signature to the .deb file (default = %(default)s)")
prod_parser.add_argument ("-i", "--install", action='store_true', dest="build_deb" , default=False, help="Build .deb file from existing structure (default = %(default)s)")
# parser for development
dev_parser = main_subparsers.add_parser("Dev", help="Dev")
dev_subparser = dev_parser.add_subparsers(title="devsubparser")
# optional development architecture choices
action_arch_parser = dev_subparser.add_parser("arch", help="architecture")
dev_arch_group = action_arch_parser.add_mutually_exclusive_group()
dev_arch_group.add_argument("-x86", action='store_const', dest="architecture", const='x', default='x',help="Build dev code on X86")
dev_arch_group.add_argument("-arm", action='store_const', dest="architecture", const='a', help="Build dev code on arm")
# development build choices - 2 arguments (coms / all) and a third (server) that has its own options.
dev_build_parser = dev_subparser.add_parser("build", help="build")
dev_build_parser.add_argument("-comms", action='store_true', help="Build comms program")
dev_build_parser.add_argument("-all", action='store_true', help="Build all programs")
server_parser = dev_build_parser.add_subparsers(title="server", help="server subparser")
server_parser_p = server_parser.add_parser("server", help="server parser")
server_parser_p.add_argument("-tcp", help="tcp option")
server_parser_p.add_argument("-fips", help="fips option")
server_parser_p.add_argument("-sim", help="sim option")
args = main_parser.parse_args()