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

查看:17
本文介绍了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) + ['']

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

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