32

I'm using argparse in Python2.7 and I would like to display multiple lines in the help text of an argument.

My codes look like this:

import argparse

parser = argparse.ArgumentParser(description='details',
        usage='use "%(prog)s --help" for more information')

parser.add_argument('--argument', default=None, type=sometype,
        help='''
             First line  \n
             Second line \n
             \n
             More lines  \n
             ''')

I would like to print out the help message in multiple lines when calling --help. However, the output looks as follows.

First line Second line More lines

I know that I could solve the problem by adding up the strings of each line.

parser.add_argument('--argument', default=None, type=sometype,
        help='First line  \n' +
             'Second line \n' +
             '\n'             + 
             'More lines')

But there are tens of lines I want to add to the help text. I was wondering is there a convenient way of splitting the help text into multiple lines ?

And also, it seems that there is an upper limit of the number of characters that can be displayed in one line in the help message, which is 54 in my case. Is this limit system-dependent and is there a way to increase the upper limit ?

Wang Zong'an
  • 1,492
  • 5
  • 24
  • 29
  • 2
    Argparse clears out newlines on purpose as it handles reflowing the text based on terminal size. – Martijn Pieters Apr 13 '15 at 19:29
  • I see, so the upper limit for the number of characters in each line is system-dependent. – Wang Zong'an Apr 13 '15 at 19:32
  • 2
    Possible duplicate of [Python argparse: How to insert newline in the help text?](http://stackoverflow.com/questions/3853722/python-argparse-how-to-insert-newline-in-the-help-text) – Trevor Boyd Smith Mar 23 '17 at 15:32
  • This is not working out for me (using python 3.6.8 on Ubuntu Linux 18.10). The linebreaks still don't show up, it's all one justified text block. – RobertG May 05 '19 at 17:30
  • I found my mistake there: If subparsers are used (add_subparsers, later add_parser to define the subparser), the formatter_class has to be re-specified for each subparser – RobertG May 06 '19 at 12:24

3 Answers3

49

The default help formatter re-wraps lines to fit your terminal (it looks at the COLUMNS environment variable to determine the output width, defaulting to 80 characters total).

From the formatter_class section:

By default, ArgumentParser objects line-wrap the description and epilog texts in command-line help messages.

Use the RawTextHelpFormatter class instead to indicate that you already wrapped the lines:

RawTextHelpFormatter maintains whitespace for all sorts of help text, including argument descriptions.

For your code that'd look like:

parser = argparse.ArgumentParser(description='details',
        usage='use "%(prog)s --help" for more information',
        formatter_class=argparse.RawTextHelpFormatter)

Do watch out you don't add too many newlines; triple-quoted strings include the newlines you leave in the string. As such you don't need the \n characters:

>>> import argparse
>>> parser = argparse.ArgumentParser(description='details',
...         usage='use "%(prog)s --help" for more information',
...         formatter_class=argparse.RawTextHelpFormatter)
>>> parser.add_argument('--argument', default=None,
...         help='''
...              First line
...              Second line
... 
...              More lines
...              ''')
_StoreAction(option_strings=['--argument'], dest='argument', nargs=None, const=None, default=None, type=None, choices=None, help='\n             First line\n             Second line\n\n             More lines\n             ', metavar=None)
>>> parser.print_help()
usage: use " --help" for more information

details

optional arguments:
  -h, --help           show this help message and exit
  --argument ARGUMENT  
                                    First line
                                    Second line

                                    More lines
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 3
    If subparsers are used (add_subparsers, later add_parser to define the subparser), the formatter_class has to be re-specified for each subparser – RobertG May 06 '19 at 12:24
  • To add on to RobertG's comment, to set the formatter class you must first construct the subparser, _then_ you can specify the formatter with `p.formatter_class = argparse.RawDescriptionHelpFormatter`. – Benjamin Carlsson Oct 01 '21 at 21:54
9

Another easy way to do it is to include textwrap.

For example,

import argparse, textwrap
parser = argparse.ArgumentParser(description='Prepare input file',
        usage='use "python %(prog)s --help" for more information',
        formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument('--argument', default=somedefault, type=sometype,
        help= textwrap.dedent('''\
        First line
        Second line
        More lines ...
         '''))

In this way, we can avoid the long empty space in front of each output line.

usage: use "python your_python_program.py --help" for more information

Prepare input file

optional arguments:
-h, --help            show this help message and exit
--argument ARGUMENT
                      First line
                      Second line
                      More lines ...
Wang Zong'an
  • 1,492
  • 5
  • 24
  • 29
-11

The simplest thing you could do would be to place the lines in an array and then join them with newlines like so:

help_lines = ['First line', 'Second line', '', 'More lines']
# ...
parser.add_argument('--argument', default=None, type=sometype,
help='\n'.join(help_lines))
Eric Scrivner
  • 274
  • 2
  • 4
  • 9
    This still results in the *exact same string value*. That's because the newlines are deliberately removed by the help formatter. – Martijn Pieters Apr 13 '15 at 19:33