【发布时间】:2019-07-11 07:59:28
【问题描述】:
我正在尝试编写一个命令行应用程序,它具有多种运行模式(类似于git 具有clone、pull 等)。我的每个子命令都有自己的选项,但我也希望它们共享一组必需的选项,所以我尝试使用父解析器来实现它。但是,似乎继承一个必需的选项会导致子解析器继续要求它。这是一个重新创建行为的示例:
import argparse
parent_parser = argparse.ArgumentParser(description="The parent parser")
parent_parser.add_argument("-p", type=int, required=True,
help="set the parent required parameter")
subparsers = parent_parser.add_subparsers(title="actions", required=True, dest='command')
parser_command1 = subparsers.add_parser("command1", parents=[parent_parser],
add_help=False,
description="The command1 parser",
help="Do command1")
parser_command1.add_argument("--option1", help="Run with option1")
parser_command2 = subparsers.add_parser("command2", parents=[parent_parser],
add_help=False,
description="The command2 parser",
help="Do command2")
args = parent_parser.parse_args()
所以现在如果我运行 python test.py 我会得到:
usage: test.py [-h] -p P {command1,command2} ...
test.py: error: the following arguments are required: -p, command
好的,到目前为止一切顺利。然后,如果我尝试使用 python test.py -p 3 仅指定 -p 选项,我会得到:
usage: test.py [-h] -p P {command1,command2} ...
test.py: error: the following arguments are required: command
但是如果我运行python test.py -p 3 command1
我明白了:
usage: test.py command1 [-h] -p P [--option1 OPTION1] {command1,command2} ...
test.py command1: error: the following arguments are required: -p, command
如果我添加另一个-p 3,它仍然要求再次指定command,然后如果我再次添加它,它会要求另一个-p 3等等。
如果我不要求 -p 选项,问题就解决了,但是有没有办法在多个子解析器之间共享所需的选项,而不只是在每个子解析器中复制粘贴它们?还是我完全走错了路?
【问题讨论】:
-
你告诉
parent_parser和parser_command1都需要一个'-p' 参数。所以你必须test.py -p 1 command1 -p 2等。每个级别都满足自己的要求。你仍然可以使用parent_parser来填充子解析器,但是定义一个单独的main_parser不需要'-p'。
标签: python-3.x argparse