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 ?
The argparse module provides a convenient interface to handle command-line arguments. It displays the generic usage of the program, help, and errors. The parse_args() function of the ArgumentParser class parses arguments and adds value as an attribute dest of the object.
The store_true option automatically creates a default value of False. Likewise, store_false will default to True when the command-line argument is not present.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With