Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python optparse,如何在使用情况输出中包含其他信息?_Python_Optparse - Fatal编程技术网

python optparse,如何在使用情况输出中包含其他信息?

python optparse,如何在使用情况输出中包含其他信息?,python,optparse,Python,Optparse,使用python的optpasse模块,我想在常规使用输出下面添加额外的示例行。我当前的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 component

使用python的optpasse模块,我想在常规使用输出下面添加额外的示例行。我当前的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
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
我希望它能为我工作中不太懂X的用户提供使用示例。大概是这样的:

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
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
我将如何做到这一点?什么样的optpass选项允许这样做?当前代码:

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

使用
用法
参数:

usage = "usage: %prog [options] arg1 arg2"
parser = OptionParser(usage=usage)
您可以通过以下方式添加更多内容(仅举一个示例):

示例输出:

用法:[选项]arg1 arg2

选项:-h,--help显示此帮助消息并退出
-v、 --冗长制造大量噪音[默认值]
-q、 ——安静,安静(我在猎杀瓦比狗)
-fFILE,--file=文件写入输出到文件
-mMODE,--mode=模式交互模式:“新手”、“中级”、“默认”、“专家”之一

危险选项:警告:使用 这些选择的风险由您自己承担。信息技术 据信,其中一些会咬人。 -g组选项


看看。

有一个
说明
参数可以传递给
OptionParser
构造函数。这允许您包括出现在
用法
之后,但在选项列表之前的任意文本

默认的
format\u epilog
会去除换行符(使用textwrap),因此您需要像这样覆盖解析器中的
format\u 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
""")
...
这里有更多的细节。
如果查看类
OptionParser
中的
optpasser.py
,则有一个名为
format\u epilog
的方法,该方法由
format\u help
调用

下面是optpasse.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\u epilog

的行为。另一种方法是禁用
-h
的默认行为,并打印您自己的帮助屏幕,其中可以包括默认屏幕:

from optparse import OptionParser

parser = OptionParser(add_help_option=False, 
                      epilog="This can't be easily\n multilined")
parser.add_option('-h', '--help', dest='help', action='store_true',
                  help='show this help message and exit')

(options, args) = parser.parse_args()

if options.help:
    parser.print_help()
    print 'now we have an epilog'
    print 'with as many lines as you wish'
    sys.exit()
这基本上就是解析器对默认行为
add\u help\u option=True
所做的,当然不包括
print
s


但是,老实说,我也更喜欢在开头和结尾简单地添加任意数量的描述行。

详细说明获胜的答案(这帮助我在自己的代码中解决了相同的问题),一个快速而肮脏的选择是直接用标识方法覆盖类的方法:

optparse.OptionParser.format_epilog = lambda self, formatter: self.epilog
optparser = optparse.OptionParser(epilog=helptext)
将帮助文本逐字打印为尾声


我认为这会覆盖程序中OptionParser类的所有用途的尾声格式,但是,当您在程序中的其他地方使用OptionParser时,所有此类尾声必须以逐字的方式传递。

我将IndentedHelpFormatter子类化,它非常简单:

class PlainHelpFormatter(optparse.IndentedHelpFormatter):
    def format_description(self, description):
        if description:
            return description + "\n"
        else:
            return ""
    def format_epilog(self, epilog):
        if epilog:
            return epilog + "\n"
        else:
            return ""

谢谢你的快速帮助!如果我想让示例出现在常规用法摘要的下面呢?非常感谢,这太棒了,因为它没有记录在optparse页面上。这将是我的第一个非bash脚本,所以请原谅我的N00B。如果我想要三到四行尾声呢?+1-你每天都能学到新东西。我知道
description
,并将其用于类似目的。这个好多了。有趣的是,它在解释程序帮助中提到,所以它在docstring中<代码>导入optpass;帮助(optparse.OptionParser)显示了它……我添加了它,效果非常好!谢谢你。就在昨天,我读了《核心Python》中关于OOP的一章,我一点也不知道这里发生了什么。在接下来的几个小时里,我会茫然地盯着它看,直到我得到它。祝我好运。epilog似乎是在2.4之后添加的:(迁移到argparse:-)