Python argparse-向多个子解析器添加参数

发布于 2021-01-29 19:29:48

我的脚本定义了一个主解析器和多个子解析器。我想将-p论点应用于一些子解析器。到目前为止,代码如下所示:

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")

如您所见,add_arument ("-p")重复了两次。实际上,我还有更多的次级解析器。有没有一种方法可以遍历现有的子解析器以避免重复?

作为记录,我正在使用Python 2.7

关注者
0
被浏览
154
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    这可以通过定义一个包含公共选项的父解析器来实现:

    import argparse
    
    parent_parser = argparse.ArgumentParser(description="The parent parser")
    parent_parser.add_argument("-p", type=int, required=True,
                               help="set db parameter")
    subparsers = parent_parser.add_subparsers(title="actions")
    parser_create = subparsers.add_parser("create", parents=[parent_parser],
                                          add_help=False,
                                          description="The create parser",
                                          help="create the orbix environment")
    parser_create.add_argument("--name", help="name of the environment")
    parser_update = subparsers.add_parser("update", parents=[parent_parser],
                                          add_help=False,
                                          description="The update parser",
                                          help="update the orbix environment")
    

    这将生成以下格式的帮助消息:

    parent_parser.print_help()
    

    输出:

    usage: main.py [-h] -p P {create,update} ...
    The parent parser
    optional arguments:
      -h, --help       show this help message and exit
      -p P             set db parameter
    actions:
      {create,update}
        create         create the orbix environment
        update         update the orbix environment
    
    
    
    parser_create.print_help()
    

    输出:

    usage: main.py create [-h] -p P [--name NAME] {create,update} ...
    The create parser
    optional arguments:
      -h, --help       show this help message and exit
      -p P             set db parameter
      --name NAME      name of the environment
    actions:
      {create,update}
        create         create the orbix environment
        update         update the orbix environment
    

    但是,如果您运行程序,则如果未指定操作(即createupdate),则不会遇到错误。如果您希望这种行为,请按如下所示修改您的代码。

    <...>
    subparsers = parent_parser.add_subparsers(title="actions")
    subparsers.required = True
    subparsers.dest = 'command'
    <...>
    

    由@hpaulj更新

    由于自2011年以来处理次解析器的变化,将主解析器用作并不是一个好主意parent。更一般而言,不要尝试dest在主解析器和子解析器中定义相同的参数(相同)。子解析器的值将覆盖主设置的所有内容(即使子解析器default也可以这样做)。创建单独的解析器以用作parents。并且如文档中所示,父母应使用add_help=False



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看