python optparse,如何在使用输出中包含附加信息?
使用 python 的 optparse 模块,我想在常规使用输出下方添加额外的示例行。我当前的 help_print() 输出如下所示:
usage: check_dell.py [options]
options:
-h, --help show this help message and exit
-s, --storage checks virtual and physical disks
-c, --chassis checks specified chassis components
我希望它包含工作中 *nix 文化程度较低的用户的使用示例。像这样的事情:
usage: check_dell.py [options]
options:
-h, --help show this help message and exit
-s, --storage checks virtual and physical disks
-c, --chassis checks specified chassis components
Examples:
check_dell -c all
check_dell -c fans memory voltage
check_dell -s
我将如何实现这一目标?哪些 optparse 选项允许这样做?当前代码:
import optparse
def main():
parser = optparse.OptionParser()
parser.add_option('-s', '--storage', action='store_true', default=False, help='checks virtual and physical disks')
parser.add_option('-c', '--chassis', action='store_true', default=False, help='checks specified chassis components')
(opts, args) = parser.parse_args()
Using python's optparse module I would like to add extra example lines below the regular usage output. My current help_print() output looks like this:
usage: check_dell.py [options]
options:
-h, --help show this help message and exit
-s, --storage checks virtual and physical disks
-c, --chassis checks specified chassis components
I would like it to include usage examples for the less *nix literate users at my work. Something like this:
usage: check_dell.py [options]
options:
-h, --help show this help message and exit
-s, --storage checks virtual and physical disks
-c, --chassis checks specified chassis components
Examples:
check_dell -c all
check_dell -c fans memory voltage
check_dell -s
How would I accomplish this? What optparse options allow for such? Current code:
import optparse
def main():
parser = optparse.OptionParser()
parser.add_option('-s', '--storage', action='store_true', default=False, help='checks virtual and physical disks')
parser.add_option('-c', '--chassis', action='store_true', default=False, help='checks specified chassis components')
(opts, args) = parser.parse_args()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
默认的
format_epilog
会去除换行符(使用 textwrap),因此您需要像这样在解析器中重写format_epilog
。这里有更多细节。
如果您查看
OptionParser
类中的optparse.py
,有一个名为format_epilog
的方法,它由format_help
调用这是来自 optparse.py 的片段。
formatter.format_epilog
的默认行为是使用textwrap.fill
,其中包括从 Epilog 中删除换行符。由于我们希望保留换行符,因此我们对 OptionParser 进行子类化并更改 format_epilog 的行为The default
format_epilog
strips the newlines (uses textwrap), so you would need to overrideformat_epilog
in your parser like this.Here's a bit more detail.
If you look in
optparse.py
in the classOptionParser
there is a method calledformat_epilog
which is called byformat_help
here is the snippet from optparse.py
The default behaviour of
formatter.format_epilog
is to usetextwrap.fill
which amongst other things, strips the newlines from the epilog. Since we want the newlines to be preserved, we subclassOptionParser
and change the behaviour offormat_epilog
详细阐述获胜的答案(这帮助我在自己的代码中解决了同样的问题),一个快速而肮脏的选择是使用标识方法直接覆盖类的方法:
将帮助文本打印为逐字结尾。
然而,我认为这会覆盖程序中所有使用 OptionParser 类的 Epilog 格式,因此在程序中其他地方使用 OptionParser 时,所有此类 Epilog 都必须逐字传递。
Elaborating on the winning answer (which helped me solve the same problem in my own code), one quick-and-dirty option is to directly override the class's method with an identity method:
to get helptext printed as a verbatim epilog.
I think this overrides the epilog formatting for all uses of the OptionParser class in your program, however, so all such epilogs must be passed in verbatim where you use OptionParser elsewhere in your program.
使用
usage
参数:您可以通过以下方式添加更多内容(仅作为示例):
示例输出:
看看这里。
Use the
usage
parameter:You can add more through (just an example):
Example output:
Have a look here.
关于如何执行此操作的另一个想法是禁用
-h
的默认行为并打印您自己的帮助屏幕,其中可以包括默认的帮助屏幕:这基本上就是解析器对
-h
的默认行为所做的操作code>add_help_option=True,当然不包括print
。但是,老实说,我还更喜欢一种在开头和结尾简单添加任意给定数量的描述行的方法。
Another idea on how to do this would be disabling the default behavior for
-h
and printing your own help screen, which can include the default one:That is basically what the parser does with the default behavior of
add_help_option=True
, excluding of course theprint
s.But, in all honesty, I'd also prefer a way to simply add any given number of description lines in the beginning and in the end.
您可以将一个
description
参数传递给OptionParser
构造函数。这允许您包含出现在usage
之后、选项列表之前的任意文本。请参阅16.4.3.1。创建解析器。
There is a
description
parameter you can pass to theOptionParser
constructor. This allows you to include arbitrary text that appears afterusage
, but before the list of options.See 16.4.3.1. Creating the parser.
我对 IndentedHelpFormatter 进行了子类化,它非常简单:
I subclassed IndentedHelpFormatter, and it was pretty simple: