【发布时间】:2015-06-11 15:04:27
【问题描述】:
使用 argparse 时,将--help 传递给程序会生成帮助文本。不幸的是,它很难阅读,因为选项之间没有空行。这里有一个摘录来说明:
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 之间,很难一眼看出哪些帮助文本适用于哪个选项。条目之间应该有一个空行。例如:
-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.
我怎样才能做到这一点? Severalother问题推荐使用argparse.RawTextHelpFormatter。问题在于,如果我使用它,我必须编写自己的逻辑来包装帮助文本,因为原始文本帮助格式化程序不进行格式化。显而易见的答案是将'\n\n' 附加到帮助文本的末尾并使用默认格式化程序。但莫名其妙地,换行符被剥离了。
这里的前进方向是什么?我正在使用 Python 3.4。
【问题讨论】: