python argparse帮助显示中的多行 [英] Multiple lines in python argparse help display

查看:23
本文介绍了python argparse帮助显示中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的代码是这样的:

导入 argparse解析器 = argparse.ArgumentParser(description='details',用法='使用 "%(prog)s --help" 获取更多信息')parser.add_argument('--argument', default=None, type=sometype,帮助='''第一行\n第二行\n\n更多行\n''')

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

第一行 第二行 更多行

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

parser.add_argument('--argument', default=None, type=sometype,help='第一行\n' +'第二行\n' +'\n' +'更多行')

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

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

解决方案

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

来自formatter_class 部分:

<块引用>

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

改用 RawTextHelpFormatter 类来指示您已经换行了:

<块引用>

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

对于看起来像这样的代码:

parser = argparse.ArgumentParser(description='details',用法='使用 "%(prog)s --help" 获取更多信息',formatter_class=argparse.RawTextHelpFormatter)

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

<预><代码>>>>导入参数解析>>>解析器 = argparse.ArgumentParser(description='details',... usage='use "%(prog)s --help" 获取更多信息',... formatter_class=argparse.RawTextHelpFormatter)>>>parser.add_argument('--argument', default=None,...帮助='''... 第一行... 第二行...... 更多线路... ''')_StoreAction(option_strings=['--argument'], dest='argument', nargs=None, const=None, default=None, type=None, choice=None, help='\n 第一行\n 第二行\n\n 更多行\n ', metavar=None)>>>parser.print_help()用法:使用--help"获取更多信息细节可选参数:-h, --help 显示此帮助信息并退出--argument 参数第一行第二行更多线路

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

这篇关于python argparse帮助显示中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆