Multiple lines in python argparse help display

后端 未结 3 2345
天涯浪人
天涯浪人 2021-02-13 00:41

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

         


        
3条回答
  •  天命终不由人
    2021-02-13 01:13

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

提交回复
热议问题