Multiple lines in python argparse help display

2019-06-17 10:24发布

问题:

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 ?

回答1:

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


回答2:

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


回答3:

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