python optparse,如何在使用输出中包含附加信息?

2024-05-15 20:37:33 发布

您现在位置:Python中文网/ 问答频道 /正文

使用python的optparse模块,我想在常规用法输出下面添加额外的示例行。我当前的帮助输出如下:

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()

Tags: andparsercheckvirtualhelpcomponentsstoragedell
5条回答
parser = optparse.OptionParser(epilog="otherstuff")

默认的format_epilog将删除换行符(使用textwrap),因此您需要这样覆盖解析器中的format_epilog

def main():

    class MyParser(optparse.OptionParser):
        def format_epilog(self, formatter):
            return self.epilog

    parser =MyParser(epilog=
"""Examples:

check_dell -c all
check_dell -c fans memory voltage
check_dell -s
""")
...

这里有更多细节。
如果您在类optparse.py中查找OptionParser,则有一个名为format_epilog的方法,该方法由format_help调用

这是optparse.py中的片段

def format_epilog(self, formatter):
    return formatter.format_epilog(self.epilog)

def format_help(self, formatter=None):
    if formatter is None:
        formatter = self.formatter
    result = []
    if self.usage:
        result.append(self.get_usage() + "\n")
    if self.description:
        result.append(self.format_description(formatter) + "\n")
    result.append(self.format_option_help(formatter))
    result.append(self.format_epilog(formatter))
    return "".join(result)

formatter.format_epilog的默认行为是使用textwrap.fill,其中包括从epilog中除去新行。因为我们希望保留新行,所以我们将OptionParser划分为子类,并改变format_epilog的行为

使用usage参数:

usage = "usage: %prog [options] arg1 arg2"
parser = OptionParser(usage=usage)

您可以通过添加更多内容(仅举一个例子):

group = OptionGroup(parser, "Dangerous Options",
                    "Caution: use these options at your own risk.  "
                    "It is believed that some of them bite.")
group.add_option("-g", action="store_true", help="Group option.")
parser.add_option_group(group)

示例输出:

usage: [options] arg1 arg2

options: -h, --help show this help message and exit
-v, --verbose make lots of noise [default]
-q, --quiet be vewwy quiet (I'm hunting wabbits)
-fFILE, --file=FILE write output to FILE
-mMODE, --mode=MODE interaction mode: one of 'novice', 'intermediate', [default], 'expert'

Dangerous Options: Caution: use of these options is at your own risk. It is believed that some of them bite. -g Group option.

看看here

在详细说明胜出的答案(这有助于我在自己的代码中解决相同的问题)时,一个快速而肮脏的选项是直接用一个identity方法重写类的方法:

optparse.OptionParser.format_epilog = lambda self, formatter: self.epilog
optparser = optparse.OptionParser(epilog=helptext)

以逐字打印帮助文本。

不过,我认为这会覆盖程序中OptionParser类的所有用法的epilog格式,因此所有这样的epilog都必须在程序的其他地方使用OptionParser时逐字传递。

相关问题 更多 >