Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #13086 > unrolled thread

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

Started byRafael Durán Castañeda <rafadurancastaneda@gmail.com>
First post2011-09-11 00:16 +0200
Last post2011-09-11 15:22 -0500
Articles 9 — 6 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: optionparse: how to add a line break to the help text Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2011-09-11 00:16 +0200
    Re: optionparse: how to add a line break to the help text "Rhodri James" <rhodri@wildebst.demon.co.uk> - 2011-09-11 02:12 +0100
      Re: optionparse: how to add a line break to the help text Gelonida N <gelonida@gmail.com> - 2011-09-11 03:54 +0200
      Re: optionparse: how to add a line break to the help text Tim Chase <python.list@tim.thechases.com> - 2011-09-10 21:08 -0500
      Re: optionparse: how to add a line break to the help text Gelonida N <gelonida@gmail.com> - 2011-09-11 05:07 +0200
        Re: optionparse: how to add a line break to the help text Ben Finney <ben+python@benfinney.id.au> - 2011-09-11 15:20 +1000
          Re: optionparse: how to add a line break to the help text Gelonida N <gelonida@gmail.com> - 2011-09-11 16:30 +0200
      Re: optionparse: how to add a line break to the help text Tim Chase <python.list@tim.thechases.com> - 2011-09-11 06:05 -0500
      Re: optionparse: how to add a line break to the help text Robert Kern <robert.kern@gmail.com> - 2011-09-11 15:22 -0500

#13086 — Re: optionparse: how to add a line break to the help text

FromRafael Durán Castañeda <rafadurancastaneda@gmail.com>
Date2011-09-11 00:16 +0200
SubjectRe: optionparse: how to add a line break to the help text
Message-ID<mailman.952.1315693006.27778.python-list@python.org>
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'

[toc] | [next] | [standalone]


#13097

From"Rhodri James" <rhodri@wildebst.demon.co.uk>
Date2011-09-11 02:12 +0100
Message-ID<op.v1mdm0oba8ncjz@gnudebst>
In reply to#13086
On Sat, 10 Sep 2011 23:16:42 +0100, Rafael Durán Castañeda  
<rafadurancastaneda@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

[toc] | [prev] | [next] | [standalone]


#13098

FromGelonida N <gelonida@gmail.com>
Date2011-09-11 03:54 +0200
Message-ID<mailman.961.1315706113.27778.python-list@python.org>
In reply to#13097
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@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)

[toc] | [prev] | [next] | [standalone]


#13099

FromTim Chase <python.list@tim.thechases.com>
Date2011-09-10 21:08 -0500
Message-ID<mailman.962.1315706938.27778.python-list@python.org>
In reply to#13097
On 09/10/11 20:54, Gelonida N wrote:
>> 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.

Just in case you want it:

http://bytes.com/topic/python/answers/734066-how-output-newline-carriage-return-optparse

it's come up several times and several years ago I hacked 
together exactly the solution Rhodri mentions.

-tkc

[toc] | [prev] | [next] | [standalone]


#13102

FromGelonida N <gelonida@gmail.com>
Date2011-09-11 05:07 +0200
Message-ID<mailman.964.1315710463.27778.python-list@python.org>
In reply to#13097
Hi Tim,


Thanks a lot!!!



On 09/11/2011 04:08 AM, Tim Chase wrote:
> On 09/10/11 20:54, Gelonida N wrote:
>>> 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.
> 
> Just in case you want it:
> 
> http://bytes.com/topic/python/answers/734066-how-output-newline-carriage-return-optparse
> 

It works (of course ;-) ) like a charm.
Good to know, that I'm not the only one who want's to structure the help
text a little nicer.

> 
> it's come up several times and several years ago I hacked together
> exactly the solution Rhodri mentions.

Considering, that you posted the snippet in 2007 and this is very
probably a reocurring problem for any slighty more complicated help text
it is really a pity, that it did not become of part of the standard
optparse library :-(


Thanks again.

[toc] | [prev] | [next] | [standalone]


#13115

FromBen Finney <ben+python@benfinney.id.au>
Date2011-09-11 15:20 +1000
Message-ID<87aaabmz94.fsf@benfinney.id.au>
In reply to#13102
Gelonida N <gelonida@gmail.com> writes:

> Considering, that you posted the snippet in 2007 and this is very
> probably a reocurring problem for any slighty more complicated help
> text it is really a pity, that it did not become of part of the
> standard optparse library :-(

The ‘optparse’ library is, as the online documentation shows
<URL:http://docs.python.org/library/optparse.html>, deprecated for new
code:

    The optparse module is deprecated and will not be developed further;
    development will continue with the argparse module.

The standard library ‘argparse’ module has a feature you might prefer
<URL:http://docs.python.org/library/argparse.html#formatter-class>.

-- 
 \      “An expert is a man who has made all the mistakes which can be |
  `\                         made in a very narrow field.” —Niels Bohr |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#13128

FromGelonida N <gelonida@gmail.com>
Date2011-09-11 16:30 +0200
Message-ID<mailman.993.1315751420.27778.python-list@python.org>
In reply to#13115
Thanks Ben,

On 09/11/2011 07:20 AM, Ben Finney wrote:
> Gelonida N <gelonida@gmail.com> writes:
> 
>> Considering, that you posted the snippet in 2007 and this is very
>> probably a reocurring problem for any slighty more complicated help
>> text it is really a pity, that it did not become of part of the
>> standard optparse library :-(
> 
> The ‘optparse’ library is, as the online documentation shows
> <URL:http://docs.python.org/library/optparse.html>, deprecated for new
> code:
> 
>     The optparse module is deprecated and will not be developed further;
>     development will continue with the argparse module.

This explains the reluctance to fix optparse. In 2007 however python 2.7
wasn't really that common though
> 
> The standard library ‘argparse’ module has a feature you might prefer
> <URL:http://docs.python.org/library/argparse.html#formatter-class>.

Most of my code has to run on python 2.5 / 2.6.

I just checked, that argparse can be installed (pip install argparse)
(didn't check functionality though) for python 2.5 / 2.6

So depending on the situation I had to decide whether I oblige users of
my scripts to  install argparse or whether I stick with optparse and
just add Tim's custom formatter.

Probably I'll go for optparse and Tim's custom formatter for tiny
scripts with no dependencies except standard libraries and
for  argparse for new bigger projects with external module dependencies.





[toc] | [prev] | [next] | [standalone]


#13119

FromTim Chase <python.list@tim.thechases.com>
Date2011-09-11 06:05 -0500
Message-ID<mailman.977.1315739164.27778.python-list@python.org>
In reply to#13097
On 09/10/11 22:07, Gelonida N wrote:
>> http://bytes.com/topic/python/answers/734066-how-output-newline-carriage-return-optparse
>
> It works (of course ;-) ) like a charm. Good to know, that I'm
> not the only one who want's to structure the help text a
> little nicer.
>
> Considering, that you posted the snippet in 2007 and this is
> very probably a reocurring problem for any slighty more
> complicated help text it is really a pity, that it did not
> become of part of the standard optparse library :-(

Even at the time, the optparse library wasn't readily patchable 
as the inline documentation gave some dire warning about "don't 
edit this directly, as this file is generated from some other 
source"—I never was able to dig up that source to patch against.

As Ben Finney replied, optparse is now deprecated, replaced in 
part by argparse. Unfortunately, argparse wasn't backported to 
the standard library for earlier 2.x series (I think it became 
available in 2.7, and may run in earlier versions if manually 
added like I had to do on my Debian Py2.6 install). But that 
makes it hard for those of us who want to use a built-in 
option-parsing library across a wide variety of Python versions. 
  I don't strongly care which I use except that I want it to be 
broadly available with minimal fuss.

-tkc

[toc] | [prev] | [next] | [standalone]


#13150

FromRobert Kern <robert.kern@gmail.com>
Date2011-09-11 15:22 -0500
Message-ID<mailman.1008.1315772560.27778.python-list@python.org>
In reply to#13097
On 9/11/11 6:05 AM, Tim Chase wrote:

> As Ben Finney replied, optparse is now deprecated, replaced in part by argparse.
> Unfortunately, argparse wasn't backported to the standard library for earlier
> 2.x series (I think it became available in 2.7, and may run in earlier versions
> if manually added like I had to do on my Debian Py2.6 install). But that makes
> it hard for those of us who want to use a built-in option-parsing library across
> a wide variety of Python versions. I don't strongly care which I use except that
> I want it to be broadly available with minimal fuss.

argparse.py can be simply dropped into your codebase, if you want. That's about 
as minimal of fuss as you can ask for.

   http://pypi.python.org/pypi/argparse

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web