Multiple lines in python argparse help display

I am using argparse in Python2.7, and I would like to display several lines in the help text of the 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 a help message in several lines when calling --help. However, the conclusion is as follows.

 First line Second line More lines 

I know that I can solve the problem by adding the lines of each line.

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

But there are dozens of lines that I want to add to the help text. I was wondering if there is a convenient way to split the help text into several lines?

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

+6
source share
4 answers

By default, the formatter will convert strings according to your terminal (it looks at the COLUMNS environment COLUMNS to determine the output width, by default no more than 80 characters).

In the formatter_class section:

By default, ArgumentParser objects wrap around epilog descriptions and texts in command line messages.

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

RawTextHelpFormatter supports spaces for all kinds of help text, including argument descriptions.

For your code, which will look like this:

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

Make sure that you do not add too many new lines; triple quotes include the lines you leave in the line. This way you do not 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 
+11
source

Another easy way to do this is to enable textwrap .

For instance,

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

This way we can avoid the long empty space before 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 ... 
+5
source

This still leads to the same string value. This is because line breaks are intentionally deleted by the help formatter. - Martin Peters

0
source

The simplest thing you could do is put the strings in an array and then append them to the new characters as follows:

 help_lines = ['First line', 'Second line', '', 'More lines'] # ... parser.add_argument('--argument', default=None, type=sometype, help='\n'.join(help_lines)) 
-2
source

Source: https://habr.com/ru/post/985085/


All Articles