Look at the Lib/optparse.py
file.
class HelpFormatter
def format_option
...
if option.help:
help_text = self.expand_default(option)
help_lines = textwrap.wrap(help_text, self.help_width)
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
result.extend(["%*s%s\n" % (self.help_position, "", line)
for line in help_lines[1:]])
In other words, your help
string is passed through textwrap.wrap
. A quick test of that function shows that it removes embedded \n
, and splits the line as it sees fit.
As with argparse
, you can customize your help by subclassing HelpFormatter
, and rewriting selected methods such as this one. For example, you might get want you want by replacing that textwrap
call with help_lines = help_text.splitlines()
. You loose the automatic wrapping, but gain control over the appearance of the help.
OptionParser
takes a formatter
parameter. IndentedHelpFormatter
is an example of customized formatter class.
The argparse
, RawTextHelpFormatter
class does just such a change, replacing a wrap
call with a splitlines()
.