3

How to string format OptionParser() help message? It seems to ignore the new line character? Please see below code.

parser = OptionParser()
parser.add_option("--s", dest="s", type="string", help="first line \n second line")

Intention:

current output:
.... first line \n second line

expected output:
.... first line 
     second line
user3388884
  • 4,748
  • 9
  • 25
  • 34
  • Possible duplicate of [displaying newlines in the help message when using python's optparse](http://stackoverflow.com/questions/5961160/displaying-newlines-in-the-help-message-when-using-pythons-optparse) – Ciro Santilli OurBigBook.com Jan 12 '17 at 11:13

2 Answers2

2

Look at the Lib/optparse.py file.

class HelpFormatter
    def format_option
    ...
    if option.help:
        help_text = self.expand_default(option)
        help_lines = textwrap.wrap(help_text, self.help_width)
        result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
        result.extend(["%*s%s\n" % (self.help_position, "", line)
                       for line in help_lines[1:]])

In other words, your help string is passed through textwrap.wrap. A quick test of that function shows that it removes embedded \n, and splits the line as it sees fit.

As with argparse, you can customize your help by subclassing HelpFormatter, and rewriting selected methods such as this one. For example, you might get want you want by replacing that textwrap call with help_lines = help_text.splitlines(). You loose the automatic wrapping, but gain control over the appearance of the help.

OptionParser takes a formatter parameter. IndentedHelpFormatter is an example of customized formatter class.

The argparse, RawTextHelpFormatter class does just such a change, replacing a wrap call with a splitlines().

hpaulj
  • 221,503
  • 14
  • 230
  • 353
1

Might I suggest argparse?

I'm not sure if this is supported in OptionParser, but I would suggest using a triple quote
i.e:

parser = OptionParser()
parser.add_option('--s',
                  dest='s'
                  type='string'
                  help='''
With triple quotes I can directly put in anything including line spaces.
\n will appear as a string rather than a newline.''')

argparse example:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--s',
                  help='''first line
second line''')
args = parser.parse_args()
print args.s
hpaulj
  • 221,503
  • 14
  • 230
  • 353
Chrispresso
  • 3,660
  • 2
  • 19
  • 31
  • i'm seeing that \n is ignored no matter what quotes i use... I need to create newlines with \n – user3388884 Jun 19 '14 at 20:26
  • The `\n` will be ignored. Simply by pressing the `Return` character on your keyboard will put a newline in there for you. – Chrispresso Jun 19 '14 at 20:30
  • wow... why did I not think of that... I was thinking there is viable solution and begin manually creating my own arguement parser – user3388884 Jun 19 '14 at 20:36
  • Try using argparse. The link I provided has some great examples. – Chrispresso Jun 19 '14 at 20:51
  • The triple quotes are just another way of inserting an `\n` into the help text string. They don't help with the original problem (with either parser). The `argparser` formatter_class has to be changed to `RawTextHelpFormatter`. – hpaulj Jun 24 '14 at 02:08