optionparse: how to add a line break to the help text

Gelonida N gelonida at gmail.com
Sun Sep 11 03:54:57 CEST 2011


Hi James,

On 09/11/2011 03:12 AM, Rhodri James wrote:
> On Sat, 10 Sep 2011 23:16:42 +0100, Rafael Durán Castañeda
> <rafadurancastaneda at gmail.com> wrote:
> 
>> On 10/09/11 22:43, Gelonida N wrote:
>>>
>>> from optparse import OptionParser
>>>
>>> parser = OptionParser()
>>> parser.add_option("-f", action="store",
>>>      help="This option is really really complicated"
>>>           " and I'd like to write"
>>>           " a few paragrpahs to explain how it works."
>>>           "\nHowever the line breaks are stripped off"
>>>           " and it's thus difficult to structure the help text")
>>>
>>> args = ['-h']
>>> parser.parse_args(args)
>>>
>>> Is there any trick to force a new paragraph/ line break before the word
>>> 'However'?
>>

> 
> Unfortunately the help text is formatted using textwrap, which presumes
> that the entire text is a single paragraph.  To get paragraphs in the
> help text, you'll need to write an IndentedHelpFormatter subclass that
> splits the text on "\n\n", textwraps the split string individually, then
> re-joins them.  _format_text() and format_option() look like the methods
> that would need replacing.
> 

Thanks a lot. Good to know, that there are options, though a little more
complicated than expected.

I'll live withou tthe line break for the time being and will deep dive
lateron, when I really want to insist on pragraphs within a help section.

I like the idea of '\n\n' as paragraph markes.
Long term this could probably even become an enhancement for  optparse
(with an explicit option to enable it in order to break no existing code)





More information about the Python-list mailing list