optparse help output

Tim Chase python.list at tim.thechases.com
Wed Oct 24 18:06:44 CEST 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:


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:


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,


More information about the Python-list mailing list