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
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.
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.
What's the way forward here? I'm using Python 3.4.