optparse help output

Tim Chase python.list at tim.thechases.com
Wed Oct 24 12:06:44 EDT 2007


> I've been using optparse for a while, and I have an option with a
> number of sub-actions I want to describe in the help section:
> 
>     parser.add_option("-a", "--action",
>                       help=\
[snipped formatted help]
> """)
> 
> Unfortunately, when I run the script with --help, this is what I get
> for the -a option:
[snipped munged formatting of help]
> Is there any way to get the formatting I want?

I had the same issue:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/6df6e6b541a15bc2/09f28e26af0699b1?#09f28e26af0699b1

and was directed by Ben Finney to check out a custom formatter. 
I got it working to my satisfaction and posted it in that thread:

http://groups.google.com/group/comp.lang.python/msg/09f28e26af0699b1

It may be a little more kind in what it does with your help-text. 
  If you need variant behavior you can take my code and mung it 
even further.

The changes are basically a copy&paste (including the comments, 
which Steven D'Aprano suggested might be better made into 
docstrings) of the format_description() and format_option() calls 
from the standard-library's source, and changing a few small 
lines to alter the behavior of calls to textwrap.*

Hope this helps,

-tkc







More information about the Python-list mailing list