默认帮助格式化程序重新格式化字符串,以便您可以根据需要在帮助字符串中使用换行符:>>> from optparse import OptionParser
>>> parser = OptionParser()
>>> parser.add_option('--my-option', help='''aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaa
... b
... c d
... e
... f''')
>>> parser.print_help()
Usage: bpython [options]
Options:
-h, --help show this help message and exit
--my-option=MY_OPTION
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaa b c d e f
要删除任何可以使用^{}的公共前导空格:>>> from optparse import OptionParser
>>> from textwrap import dedent
>>> parser = OptionParser()
>>> parser.add_option('--my-option', help=dedent('''\
... Here is a long description of my option. It does many things
... but I want the shell to decide how to display this
... explanation. However, I want newlines in this string.'''))
>>> parser.print_help()
Usage: [options]
Options:
-h, --help show this help message and exit
--my-option=MY_OPTION
Here is a long description of my option. It does many
things but I want the shell to decide how to display
this explanation. However, I want newlines in this
string.