ArgParse:是否可以将帮助来自多个解析器的帮助文本组合在一起?
我正在编写一个模块,其中包含在其他脚本中导入的自定义记录实用程序。 它基于标准图案记录模块。
这些公用事业之一看起来像这样:
import argparse as ap
def parse_log_args() -> dict:
log_arg_parser = ap.ArgumentParser(description='Parses arguments that affect logging')
log_arg_parser.add_argument(
'--level',
dest='level',
help='Sets logging level',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
)
log_args, _ = log_arg_parser.parse_known_args()
return vars(log_args)
此功能寻找与记录有关的参数(即使仅是暂时定义的),并且独立于(和之前)所有其他的解析这些论点,以便日志记录可以是早期配置并在其余脚本中使用。
这里的目的是保持灵活性,并能够在没有其他参数的脚本中快速为这些参数提供插入支持。
从简单解析参数的角度来看,这起作用:此函数首先运行,解析 - 级别,然后脚本特定的解析器来处理其余的。
但是,问题是帮助文本。当我运行一个使用脚本调用此函数的脚本时 - 螺旋,它仅显示第一个解析器的帮助文本,而不是从特定于脚本的文本中显示。因此,这样的事情是:
Parses arguments that affect logging
optional arguments:
-h, --help show this help message and exit
--level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Sets logging level
- 有没有办法将脚本中所有参数范围实例的帮助文本组合在一起?
- 另外:是否有一种以灵活/插件的方式实现此目标的方法,也就是说,不修改现有的参数parsers或必须将它们添加到尚未使用它们的脚本中?
PS:在此之前提出了类似的问题: argparse combine combine-help Directives 想法并不能真正解决问题:
- 用add_help = false定义第一个解析器:这将使我宁愿不这样做的用户隐藏选项。
- 以某种方式使用子命令:这里似乎不适用。
I'm writing a module with custom logging utilities to be imported in other scripts.
It's based on the standard-library logging module.
One of these utilities looks like this:
import argparse as ap
def parse_log_args() -> dict:
log_arg_parser = ap.ArgumentParser(description='Parses arguments that affect logging')
log_arg_parser.add_argument(
'--level',
dest='level',
help='Sets logging level',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
)
log_args, _ = log_arg_parser.parse_known_args()
return vars(log_args)
This function looks for arguments that have to do with logging (even though only --level is defined for the time being) and parses those independently of (and before) all others so that the logging can be configured early on and used in the rest of the script.
The goal here is to remain flexible and be able to quickly plug-in support for these arguments, both in scripts that expect no other arguments and in those that do.
From the point of view of simply parsing arguments this works: this function runs first, parses --level and then the script-specific parser comes and handles the rest.
The problem, however, is the help text. When I run a script that calls this function with --help it only displays the help text from this first parser and not from the script-specific one. So something like this:
Parses arguments that affect logging
optional arguments:
-h, --help show this help message and exit
--level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Sets logging level
- Is there a way to combine the help-texts from all the ArgumentParser instances in a script?
- Alternatively: Is there a different way to achieve this in a flexible/plug-in kind of way, that is, without modifying existing ArgumentParsers or having to add them to scripts that don't yet use them?
PS: A similar question has been asked before here: Argparse combine --help directives but the proposed ideas don't really solve the problem:
- Define the first parser with add_help=False: This would hide the option from the user which I would prefer not to do.
- Use subcommands somehow: doesn't seem to be applicable here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能符合账单:
part1
解析器必须完全初始化父母
工作。有关该主题的更多信息:
https://docs.python.org/3/library/library/argparse.html#父母
I think this might fit the bill:
part1
parser must be fully initialized forparents
to work.More on the topic:
https://docs.python.org/3/library/argparse.html#parents