Python argparse - 将父解析器参数分组 [英] Python argparse - Grouping parent parser arguments into groups
问题描述
有没有办法将来自父解析器的参数分组到不同的组中?我无权访问父解析器本身,所以我无法在那里添加组.(我使用的是 Google 的 OAuth2 框架).
Is there a way to group the arguments from parent parsers into different groups? I don't have access to the parent parser itself, so I can't add the group there. (I'm using Google's OAuth2 framework).
目前我的代码是:
# test.py
from argparse import ArgumentParser
from oauth2client import tools
parser = ArgumentParser(description="My program", parents=[tools.argparser])
parser.add_argument("--foo", help="Foo the data")
parser.add_argument("--bar", help="Bar the data")
parser.parse_args()
产生以下帮助:
$ python test.py -h
usage: test.py [-h] [--auth_host_name AUTH_HOST_NAME]
[--noauth_local_webserver]
[--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
[--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
[--foo FOO] [--bar BAR]
My program
optional arguments:
-h, --help show this help message and exit
--auth_host_name AUTH_HOST_NAME
Hostname when running a local web server.
--noauth_local_webserver
Do not run a local web server.
--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]
Port web server should listen on.
--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Set the logging level of detail.
--foo FOO Foo the data
--bar BAR Bar the data
所以,我想为来自父解析器的参数创建一个组.是否可以将参数组合成这样?
So, I would like to create a group for arguments from the parent parser. Is is possible to group the arguments to look something like this?
optional arguments:
-h, --help show this help message and exit
--foo FOO Foo the data
--bar BAR Bar the data
authentication options:
--auth_host_name AUTH_HOST_NAME
Hostname when running a local web server.
--noauth_local_webserver
Do not run a local web server.
--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]
Port web server should listen on.
--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Set the logging level of detail.
我知道解析器组,但我需要以某种方式将参数从一个地方获取到另一个地方,例如:
I know about parser groups, but I need to somehow get the arguments from one place to another something like:
auth_group = parser.add_argument_group('authentication options')
for arg in get_args_from_parser(tools.argparser):
auth_group.add_argument(arg)
但我找不到一种方法来列出这样的参数或将它们从一个地方复制到另一个地方.
But I can't find a way to list arguments like that or copy them from one place to another.
推荐答案
感谢@hpaulj 和问题重新排序 Python argparse 参数组"
Thanks to @hpaulj and the question "Reorder Python argparse argument groups"
执行此操作的方法取决于 argparse 的实现细节,正如我可能应该预料到的,因此使用风险自负.但这适用于当前的 Python 3.7.0.
The way to do this depends on the implementation details of argparse, as I probably should have expected, so use at your own risk. But this works with the current Python 3.7.0.
我们可以重命名父解析器中的组,然后再将其添加到我自己的:
We can rename the group(s) in the parent parser before adding it to my own:
# test.py
from argparse import ArgumentParser
from oauth2client import tools
tools.argparser._action_groups[1].title = 'authentication options'
parser = ArgumentParser(description="My program", parents=[tools.argparser])
parser.add_argument("--foo", help="Foo the data")
parser.add_argument("--bar", help="Bar the data")
parser.parse_args()
输出结果:
$ python test.py -h
usage: test.py [-h] [--auth_host_name AUTH_HOST_NAME]
[--noauth_local_webserver]
[--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
[--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
[--foo FOO] [--bar BAR]
My program
optional arguments:
-h, --help show this help message and exit
--foo FOO Foo the data
--bar BAR Bar the data
authentication options:
--auth_host_name AUTH_HOST_NAME
Hostname when running a local web server.
--noauth_local_webserver
Do not run a local web server.
--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]
Port web server should listen on.
--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Set the logging level of detail.
这是通过重命名父解析器中的默认组之一来实现的.这些组存储在 parser._action_groups
中,并按顺序 ['positional arguments', 'optional arguments']
所以我们重命名第二个(这些参数就在这里是) 在它被添加到新的解析器之前.这也会阻止合并组.
This works by renaming one of the default groups in the parent parser. The groups are stored in parser._action_groups
, and are ordered ['positional arguments', 'optional arguments']
So we are renaming the second one (which is where these arguments are) before it is added to the new parser. This also stops the groups from being merged.
这篇关于Python argparse - 将父解析器参数分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!