Python argparse - Add argument to multiple subparsers

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

My script defines one main parser and multiple subparsers. I want to apply the -p argument to some subparsers. So far the code looks like this:

parser = argparse.ArgumentParser(prog="myProg") subparsers = parser.add_subparsers(title="actions")  parser.add_argument("-v", "--verbose",                     action="store_true",                     dest="VERBOSE",                     help="run in verbose mode")  parser_create = subparsers.add_parser ("create",                                          help = "create the orbix environment") parser_create.add_argument ("-p",                              type = int,                              required = True,                              help = "set db parameter")  # Update parser_update = subparsers.add_parser ("update",                                          help = "update the orbix environment") parser_update.add_argument ("-p",                              type = int,                              required = True,                              help = "set db parameter") 

As you can see the add_arument ("-p") is repeated twice. I actually have a lot more subparsers. Is there a way to loop through the existing subparsers in order to avoid repetition?

For the record, I am using Python 2.7

回答1:

This can be achieved by defining a parent parser containing the common option(s):

[...] parent_parser = argparse.ArgumentParser(add_help=False) parent_parser.add_argument("-p", type=int, required=True,                            help="set db parameter") parser_create = subparsers.add_parser("create", parents=[parent_parser],                                       help="create the orbix environment") parser_update = subparsers.add_parser("update", parents=[parent_parser],                                       help="update the orbix environment") 


回答2:

You can also loop over the subparsers and add the same option to all of them.

parser = argparse.ArgumentParser(prog="myProg") subparsers = parser.add_subparsers(title="actions") parser.add_argument("-v", "--verbose",                     action="store_true",                     dest="VERBOSE",                     help="run in verbose mode")  parser_create = subparsers.add_parser ("create",                                          help = "create the orbix environment") parser_update = subparsers.add_parser ("update",                                          help = "update the orbix environment")  for subparser in [parser_create, parser_update]:     subparser.add_argument ("-p",                              type = int,                              required = True,                              help = "set db parameter") 


回答3:

You can loop over your subparsers in the folowing way.

for name, subp in subparsers.choices.items():     print(subp)     subp.add_argument(dest='g', help='Inpput for g variable', default='7') 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!