Python argparse - 向多个子解析器添加参数
我的脚本定义了一个主解析器和多个子解析器。我想将 -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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由 @hpaulj 更新
由于自 2011 年以来处理子解析器的变化,使用主解析器作为
父
是一个坏主意。更一般地说,不要尝试在主解析器和子解析器中定义相同的参数(相同的dest
)。子解析器值将覆盖主解析器设置的任何内容(即使子解析器default
也会这样做)。创建单独的解析器用作父级
。如文档中所示,家长应使用add_help=False
。原始答案
这可以通过定义包含公共选项的 父解析器 来实现( s):
这会产生以下格式的帮助消息:
输出:
输出:
但是,如果您运行程序,如果不指定操作(即
create
或更新
)。如果您需要这种行为,请按如下方式修改您的代码。此修复是在这个SO问题中提出的,它指的是跟踪拉取请求的问题。
Update by @hpaulj
Due to changes in handling subparsers since 2011, it is a bad idea to use the main parser as a
parent
. More generally, don't try to define the same argument (samedest
) in both main and sub parsers. The subparser values will overwrite anything set by the main (even the subparserdefault
does this). Create separate parser(s) to use asparents
. And as shown in the documentation, parents should useadd_help=False
.Original answer
This can be achieved by defining a parent parser containing the common option(s):
This produces help messages of the format:
Output:
Output:
However, if you run your program, you will not encounter an error if you do not specify an action (i.e.
create
orupdate
). If you desire this behavior, modify your code as follows.This fix was brought up in this SO question which refers to an issue tracking a pull request.
接受的答案是正确的;正确的方法是使用父解析器。然而,示例代码在我看来并没有真正解决问题。让我添加几分钱来提供一个更合适的例子。
与接受的答案的主要区别是明确可能有一些根级参数(如
--verbose
)以及仅用于某些子解析器的共享参数(-p
仅适用于create
和update
子解析器,但不适用于其他子解析器)这是顶级帮助消息(请注意
-p
code> 参数此处未显示,这正是您想要的期望 - 因为它特定于某些子解析器):以及
create
操作的帮助消息:The accepted answer is correct; the proper way is to use parent parsers. However, the example code IMO was not really solving the problem. Let me add my few cents to provide a more suitable example.
The main difference with accepted answer is the explicit possibility to have some root-level arguments (like
--verbose
) as well as shared arguments only for some subparsers (-p
only for thecreate
andupdate
subparsers but not for others)This is the top-level help message (note that
-p
parameter is not shown here, which is exactly what you would expect—because it is specific to some subparsers):And the help message for the
create
action:您还可以循环子解析器并向所有子解析器添加相同的选项。
You can also loop over the subparsers and add the same option to all of them.
您可以通过以下方式循环子解析器。
请注意,通过使用 subparsers.choices ,您可以避免对所有子解析器进行硬编码。
You can loop over your subparsers in the following way.
Note that by using
subparsers.choices
that you avoid needing to hard-code all of the subparsers.