kennytm kennytm - 2 years ago 109
Python Question

Python argparse: How to insert newline in the help text?

I'm using

in Python 2.7 for parsing input options. One of my options is a multiple choice. I want to make a list in its help text, e.g.

from argparse import ArgumentParser

parser = ArgumentParser(description='test')

parser.add_argument('-g', choices=['a', 'b', 'g', 'd', 'e'], default='a',
help="Some option, where\n"
" a = alpha\n"
" b = beta\n"
" g = gamma\n"
" d = delta\n"
" e = epsilon")


strips all newlines and consecutive spaces. The result looks like

~/Downloads:52$ python2.7 -h
usage: [-h] [-g {a,b,g,d,e}]


optional arguments:
-h, --help show this help message and exit
-g {a,b,g,d,e} Some option, where a = alpha b = beta g = gamma d = delta e
= epsilon

How to insert newlines in the help text?

Answer Source

Try using RawTextHelpFormatter:

from argparse import RawTextHelpFormatter
parser = ArgumentParser(description='test', formatter_class=RawTextHelpFormatter)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download