Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

optparse help output

440 views
Skip to first unread message

Dan

unread,
Oct 24, 2007, 11:47:15 AM10/24/07
to
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=\
"""Current supported actions: create, build, import, exp_cmd and
interact.

create -- Vaguely depreciated, should create a new project, but it
is
not currently suppored. First create a project in SVN,
then
use import.

build -- Build the project (invoking make usually).

import -- Put the project under metaman control. Assumes that
the current working directory is a SVN-controlled
sandbox. Metaman checks out its own copy, does analysis
to determine dependencies and the metadata to be
collected.

interact -- Creates a MetaMan object and starts the python interactive
interpreter. Designed for debugging or advanced usage.
The MetaMan object is bound to the identifier 'mm'. Only
use this option if you know what you're doing.

exp_cmd -- add an experiment for strataman.
""")

Unfortunately, when I run the script with --help, this is what I get
for the -a option:
-aACTION, --action=ACTION
Current supported actions: create, build,
import,
exp_cmd and interact. create -- Vaguely
depreciated,
should create a new project, but it
is not
currently suppored. First create a project in
SVN, then
use import. build -- Build the project
(invoking
make usually). import -- Put the project
under
metaman control. Assumes that the
current
working directory is a SVN-controlled
sandbox. Metaman checks out its own copy, does
analysis
to determine dependencies and the metadata to
be
collected. interact -- Creates a MetaMan
object and
starts the python interactive
interpreter.
Designed for debugging or advanced usage.
The MetaMan object is bound to the identifier
'mm'.
Only use this option if you know
what
you're doing. exp_cmd -- add an experiment
for
strataman.

Is there any way to get the formatting I want?

-Dan

Tim Chase

unread,
Oct 24, 2007, 12:06:44 PM10/24/07
to Dan, pytho...@python.org
> 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


Dan

unread,
Oct 24, 2007, 1:26:03 PM10/24/07
to
On Oct 24, 12:06 pm, Tim Chase <python.l...@tim.thechases.com> wrote:
> > 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/...

>
> 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

Thanks, Tim!

That was incredibly helpful. I did alter it to format paragraphs, but
maintain the double newlines. Also, I made a few changes to work with
the 2.3 version of optparse. Posted below for anyone who might want
it. (Also to work as a standalone .py file)

-Dan


# From Tim Chase via comp.lang.python.

from optparse import IndentedHelpFormatter
import textwrap

class IndentedHelpFormatterWithNL(IndentedHelpFormatter):
def format_description(self, description):
if not description: return ""
desc_width = self.width - self.current_indent
indent = " "*self.current_indent
# the above is still the same
bits = description.split('\n')
formatted_bits = [
textwrap.fill(bit,
desc_width,
initial_indent=indent,
subsequent_indent=indent)
for bit in bits]
result = "\n".join(formatted_bits) + "\n"
return result

def format_option(self, option):
# The help for each option consists of two parts:
# * the opt strings and metavars
# eg. ("-x", or "-fFILENAME, --file=FILENAME")
# * the user-supplied help string
# eg. ("turn on expert mode", "read data from FILENAME")
#
# If possible, we write both of these on the same line:
# -x turn on expert mode
#
# But if the opt string list is too long, we put the help
# string on a second line, indented to the same column it would
# start in if it fit on the first line.
# -fFILENAME, --file=FILENAME
# read data from FILENAME
result = []
opts = option.option_strings
opt_width = self.help_position - self.current_indent - 2
if len(opts) > opt_width:
opts = "%*s%s\n" % (self.current_indent, "", opts)
indent_first = self.help_position
else: # start help on same line as opts
opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
indent_first = 0
result.append(opts)
if option.help:
help_text = option.help
# Everything is the same up through here
help_lines = []
help_text = "\n".join([x.strip() for x in
help_text.split("\n")])
for para in help_text.split("\n\n"):
help_lines.extend(textwrap.wrap(para, self.help_width))
if len(help_lines):
# for each paragraph, keep the double newlines..
help_lines[-1] += "\n"
# Everything is the same after here
result.append("%*s%s\n" % (
indent_first, "", help_lines[0]))
result.extend(["%*s%s\n" % (self.help_position, "", line)
for line in help_lines[1:]])
elif opts[-1] != "\n":
result.append("\n")
return "".join(result)

Steven Bethard

unread,
Oct 24, 2007, 2:24:36 PM10/24/07
to
Dan wrote:
> On Oct 24, 12:06 pm, Tim Chase <python.l...@tim.thechases.com> wrote:
>>> 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/...
>>
>> 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
>>
> That was incredibly helpful. I did alter it to format paragraphs, but
> maintain the double newlines. Also, I made a few changes to work with
> the 2.3 version of optparse. Posted below for anyone who might want
> it. (Also to work as a standalone .py file)

You might consider posting it to the optik tracker:

http://optik.sourceforge.net/

A lot of people have had similar requests.

Steve

0 new messages