optionparse: how to add a line break to the help text
Rhodri James
rhodri at wildebst.demon.co.uk
Sat Sep 10 21:12:26 EDT 2011
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:
>> I'm having a small question about optionparse.
>>
>> Normaly optionparser will format the help text according to the
>> console's width.
>>
>> I just wondered if there is any way to insert a line breakk into an
>> options help text.
>>
>> Example:
>>
>> 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'?
>>
>>
>> Thanks in advance for suggestions.
>>
>>
> You can use """ for multiple line texts:
> >>> text = \
> ... """fsdfsfsdfsdf
> ... sfsdfsfsdf
> ... sdfsdf s
> ...
> ... """
> >>> text
> 'fsdfsfsdfsdf\n sfsdfsfsdf\nsdfsdf s\n\n'
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.
--
Rhodri James *-* Wildebeest Herder to the Masses
More information about the Python-list
mailing list