使用python的optparse时在帮助消息中显示换行符 [英] displaying newlines in the help message when using python's optparse

查看:176
本文介绍了使用python的optparse时在帮助消息中显示换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用optparse模块进行选项/参数解析.出于向后兼容性的原因,我不能使用argparse模块.如何格式化我的Epilog消息,以便保留换行符?

I'm using the optparse module for option/argument parsing. For backwards compatibility reasons, I can't use the argparse module. How can I format my epilog message so that newlines are preserved?

在下面的示例中,我希望将结语打印为格式化的格式.

In the below example, I'd like the epilog to be printed as formatted.

    epi = \
"""
Examples usages:
  Do something
  %prog -a -b foo
  Do something else
  %prog -d -f -h bar
"""
    parser = optparse.OptionParser(epilog=epi)

推荐答案

在以下位置查看第一个答案:

See the first answer at:

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

基本答案是将OptionParser子类化

The basic answer is to subclass the OptionParser

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

这篇关于使用python的optparse时在帮助消息中显示换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆