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

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

问题描述

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

I'm using argparse in Python2.7 and I would like to display multiple lines in the help text of an argument.

我的代码如下:

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

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

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 ?

而且,在帮助消息的一行中可以显示的字符数似乎有上限,在我的情况下为54.此限制是否与系统有关,是否有增加上限的方法?

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 ?

推荐答案

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

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

formatter_class部分中:

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

By default, ArgumentParser objects line-wrap the description and epilog texts in command-line help messages.

使用RawTextHelpFormatter类代替,表明您已经包装了以下行:

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

RawTextHelpFormatter维护各种帮助文本的空白,包括参数说明.

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)

注意不要添加太多换行符;用三引号括起来的字符串包括换行符.因此,您不需要\n字符:

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天全站免登陆