views:

97

answers:

2

I'm making a shell script with the optparse module, jut for fun, so I wanted to print a nice ascii drawing in place of the description.

Turns out that this code:

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.''')

renders like this:

$ ./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

I've tried a varying combination of slashes, newlines and espaces without success.

Can you, friend pytonista, help me display Totoro properly?

+6  A: 

The default formatter, IndentedHelpFormatter, calls this method:

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

If you subclass IndentedHelpFormatter, you can remove the self._format_text call which is causing the problem:

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()
unutbu
Google translate says the epilog means: "Cockroaches were able to successfully plan, throwing the stinking hot blood of their concrete boxes. Now the man is a species of wandering in space, a wanderer in the stars." LOL!
unutbu
Seems like what I'm looking for, thank you very much, sir.The translation is almost accurate :)
tutuca
You're very welcome, tutuca. Your epilog is intriguing! :)
unutbu
A: 

If all else fails, use code generation.

The simplest way would be to create a text file containing your desired output, and base64 encode it and embed it in a .py file that exposes a global variable

Now you need to include the generated .py, base64 decode, and print the global variable and it all works.

Joshua
You are kidding me. Right? Although I appreciate the spirit of your proposal :)
tutuca
No, I'm not kidding. There's gotta be a better way but if you can't find it, this works.
Joshua