Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASCII art in the optparse description

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?

like image 935
tutuca Avatar asked Aug 22 '10 21:08

tutuca


People also ask

What is Optparse used for?

optparse uses a more declarative style of command-line parsing: you create an instance of OptionParser , populate it with options, and parse the command line. optparse allows users to specify options in the conventional GNU/POSIX syntax, and additionally generates usage and help messages for you.

How to create opt in python?

We can create an OptionParser instance, having the optionparser properties to parse command-line arguments, using the following line of codes in any Python program: # Importing the optparse module. import optparse as opt. # Creating an OptionParser instance.


2 Answers

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()
like image 87
unutbu Avatar answered Sep 18 '22 08:09

unutbu


Sorry for the thread necromancy, but for those who upgraded to 2.7 you can now easily display ascII art in your description by simply passing

formatter_class=argparse.RawDescriptionHelpFormatter

to argparse.ArgumentParser()

see http://docs.python.org/2/library/argparse.html#formatter-class for example!

like image 23
Stephan Tual Avatar answered Sep 22 '22 08:09

Stephan Tual