Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (45.3k points)
edited by

I'm using argparse 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")


However, argparse 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 do I insert newlines in the helptext.

1 Answer

0 votes
by (16.8k points)

Try using RawTextHelpFormatter here:

from argparse import RawTextHelpFormatter

parser = ArgumentParser(description='test', formatter_class=RawTextHelpFormatter)

Related questions

+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 5, 2019 in Python by selena (1.6k points)

Browse Categories