使用 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
and -s
或之间-c
and --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.
我怎样才能做到这一点?Several https://stackoverflow.com/q/3853722/713735 other https://stackoverflow.com/q/15530575/713735问题建议使用argparse.RawTextHelpFormatter
。问题是,如果我使用它,我必须编写自己的逻辑来包装帮助文本,因为原始文本帮助格式化程序不进行格式化。显而易见的答案是附加'\n\n'
到帮助文本的末尾并使用默认格式化程序。但令人费解的是,换行符被删除了。
这里的前进方向是什么?我正在使用Python 3.4。