python argparse中的多行帮助显示

Wan*_*'an 9 python argparse

在Python2.7中使用argparse,我想在参数的帮助文本中显示多行.

我的代码如下:

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
             ''')
Run Code Online (Sandbox Code Playgroud)

我想在调用--help时多行打印出帮助信息.但是,输出如下所示.

First line Second line More lines
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过将每行的字符串相加来解决问题.

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

但是我想在帮助文本中添加几十行.我想知道有没有一种方便的方法将帮助文本分成多行?

而且,似乎帮助消息中的一行中可以显示的字符数有一个上限,在我的情况下为54.这种限制是否依赖于系统,是否有办法增加上限?

Mar*_*ers 19

默认的帮助格式化程序重新包装行以适合您的终端(它查看COLUMNS环境变量以确定输出宽度,默认为总共80个字符).

formatter_class部分:

默认情况下,ArgumentParser对象在命令行帮助消息中对描述和结语文本进行换行.

使用RawTextHelpFormatter该类来表示您已经包装了这些行:

RawTextHelpFormatter为各种帮助文本维护空格,包括参数描述.

对于你的代码看起来像:

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

请注意,不要添加太多换行符; 三引号字符串包括您在字符串中留下的换行符.因此,您不需要\n角色:

>>> 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
Run Code Online (Sandbox Code Playgroud)

  • 如果使用子解析器(add_subparsers,稍后使用 add_parser 来定义子解析器),则必须为每个子解析器重新指定 formatter_class (3认同)

Wan*_*'an 5

另一种简单的方法是包含textwrap.

例如,

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 ...
         '''))
Run Code Online (Sandbox Code Playgroud)

这样,我们可以避免每条输出线前面的长空间.

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 ...
Run Code Online (Sandbox Code Playgroud)

  • dedent 删除换行符 (3认同)