【问题标题】:ASCII art in the optparse descriptionoptparse 描述中的 ASCII 艺术
【发布时间】:2011-04-02 09:14:36
【问题描述】:

我正在使用 optparse 模块制作一个 shell 脚本,只是为了好玩,所以我想打印一个漂亮的 ascii 绘图来代替描述。

原来是这段代码:

parser = optparse.OptionParser(
    prog='./spill.py',
    description=u'''
  /     \                                     
  vvvvvvv  /|__/|                             
      I   /O,O   |                            
      I /_____   |      /|/|                 
     J|/^ ^ ^ \  |    /00  |    _//|          
      |^ ^ ^ ^ |W|   |/^^\ |   /oo |         
       \m___m__|_|    \m_m_|   \mm_|         
''',
    epilog='''
        Las cucarachas lograron con exito su plan, echando a los pestilentes sangre caliente de sus cajas de cemento. 
Ahora el hombre es una especie errante en el espacio, un vagabundo errante en las estrellas.''')

呈现如下:

$ ./bin/spill.py -h
Usage: ./spill.py [options]

   /     \                                        vvvvvvv  /|__/|
I   /O,O   |                                   I /_____   |      /|/|
J|/^ ^ ^ \  |    /00  |    _//|                 |^ ^ ^ ^ |W|   |/^^\ |   /oo |
\m___m__|_|    \m_m_|   \mm_|

Options:
  -h, --help            show this help message and exit
#.... bla bla bla, etc

我尝试了斜线、换行符和空格的不同组合,但没有成功。

pytonista 朋友,你能帮我正确显示龙猫吗?

【问题讨论】:

    标签: python optparse ascii-art


    【解决方案1】:

    默认格式化程序 IndentedHelpFormatter 调用此方法:

     def format_description(self, description):
        if description:
            return self._format_text(description) + "\n"
        else:
            return ""
    

    如果您将IndentedHelpFormatter 子类化,则可以删除导致问题的self._format_text 调用:

    import optparse
    
    class PlainHelpFormatter(optparse.IndentedHelpFormatter): 
        def format_description(self, description):
            if description:
                return description + "\n"
            else:
                return ""
    
    parser = optparse.OptionParser(
        prog='./spill.py',
        formatter=PlainHelpFormatter(),
        description=u'''
      /     \                                     
      vvvvvvv  /|__/|                             
          I   /O,O   |                            
          I /_____   |      /|/|                 
         J|/^ ^ ^ \  |    /00  |    _//|          
          |^ ^ ^ ^ |W|   |/^^\ |   /oo |         
           \m___m__|_|    \m_m_|   \mm_|         
    ''',
        epilog='''
            Las cucarachas lograron con exito su plan, echando a los pestilentes sangre caliente de sus cajas de cemento. 
    Ahora el hombre es una especie errante en el espacio, un vagabundo errante en las estrellas.''')
    (opt,args) = parser.parse_args()
    

    【讨论】:

    • 谷歌翻译说,结语的意思是:“蟑螂能够成功地计划,把它们混凝土盒子里散发出的臭热血洒在身上。现在这个人是一种在太空中游荡的物种,是在星空中的游荡者。 "大声笑!
    • 好像是我要找的东西,非常感谢,先生。翻译几乎是准确的:)
    【解决方案2】:

    如果一切都失败了,使用代码生成。

    最简单的方法是创建一个包含所需输出的文本文件,然后对其进行 base64 编码并将其嵌入到公开全局变量的 .py 文件中

    现在您需要包含生成的 .py、base64 解码并打印全局变量,一切正常。

    【讨论】:

    • 你在开玩笑吧。正确的?虽然我很欣赏你提议的精神:)
    • 不,我不是在开玩笑。一定有更好的方法,但如果你找不到它,这行得通。
    【解决方案3】:

    很抱歉线程死灵,但对于那些升级到 2.7 的人,您现在可以通过简单地在描述中轻松显示 ascII 艺术

    formatter_class=argparse.RawDescriptionHelpFormatter
    

    到 argparse.ArgumentParser()

    http://docs.python.org/2/library/argparse.html#formatter-class 为例!

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 2016-10-12
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      相关资源
      最近更新 更多