Python argparse:在帮助条目之间插入空白行 [英] Python argparse: Insert blank line between help entries

查看:95
本文介绍了Python argparse:在帮助条目之间插入空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用argparse时,将--help传递给程序会生成帮助文本.不幸的是,由于选项之间没有空白行,因此很难阅读.这是摘录来说明:

When using argparse, passing --help to the program generates help text. Unfortunately, it's hard to read because there are no blank lines between options. Here's an excerpt to illustrate:

optional arguments:
  -h, --help            show this help message and exit
  -u FILENAME, --up-sound FILENAME
                        The sound to play when the network comes up. Default:
                        "/path/to/some/sound/file.wav"
  -d FILENAME, --down-sound FILENAME
                        The sound to play when the network goes down. Default:
                        "/path/to/some/other/sound/file.wav"
  -p EXECUTABLE, --player EXECUTABLE
                        The program to use to play sounds. Default: "play"
  -s, --silent          If specified, network_monitor.py will not play any
                        sounds.
  -c, --no-clear-screen
                        If specified, screen will not be cleared (nor extra
                        blank lines added) before network_monitor.py runs.
  --version             show program's version number and exit

请注意,在某些情况下,例如-p-s之间或-c--version之间,很难一目了然地知道哪个帮助文本适用于哪个选项.条目之间应该有一个空白行.例如:

Notice that in some cases, such as between -p and -s or between -c and --version, it is difficult to tell at a glance which help text applies to which option. There should be a blank line between entries. For example:

  -p EXECUTABLE, --player EXECUTABLE
                        The program to use to play sounds. Default: "play"

  -s, --silent          If specified, network_monitor.py will not play any
                        sounds.

我该怎么做? 几个 其他问题建议使用argparse.RawTextHelpFormatter.这样做的问题在于,如果我使用它,我必须编写自己的逻辑来包装帮助文本,因为原始文本帮助格式化程序不会进行格式化.显而易见的答案是将'\n\n'附加到帮助文本的末尾,并使用默认的格式化程序.但莫名其妙的是,换行符被剥夺了.

How can I accomplish this? Several other questions recommend using argparse.RawTextHelpFormatter. The problem with that is that if I use it, I have to write my own logic to wrap the help text as the raw text help formatter does no formatting. The obvious answer would be to append '\n\n' to the end of the help text and use the default formatter. But inexplicably, newlines get stripped.

这里的前进方向是什么?我正在使用Python 3.4.

What's the way forward here? I'm using Python 3.4.

推荐答案

您可以创建自己的帮助文本格式化程序来执行此操作.请注意,这要求您非常特定于argparse.HelpFormatter的实现细节.因此,请考虑每个帮助格式化程序类型描述中都包含的警告:

You can create your own help text formatter that does this. Note that this requires you to be very specific to the implementation detail of the argparse.HelpFormatter. So consider this warning that is included in every help formatter type description:

仅此类的名称被认为是公共API.该类提供的所有方法都被视为实现细节.

Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail.

一旦我们忽略了这一点,创建自己的帮助格式化程序就可以在条目之间添加空白行,这很简单:

Once we ignore that, creating our own help formatter that adds a blank line between the entries is very simple:

class BlankLinesHelpFormatter (argparse.HelpFormatter):
    def _split_lines(self, text, width):
        return super()._split_lines(text, width) + ['']

就是这样.现在,当您将 formatter_class=BlankLinesHelpFormatter传递给构造函数时,创建 ArgumentParser 对象时,帮助文本中的每个参数之间将出现空白行.

And that’s it. Now when you create the ArgumentParser object while passing formatter_class=BlankLinesHelpFormatter to the constructor, blank lines will appear between each argument in the help text.

这篇关于Python argparse:在帮助条目之间插入空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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