Optparse and help formatting?

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Sep 30 00:15:09 EDT 2007


Tim Chase <python.list at tim.thechases.com> writes:

> I've been learning the ropes of the optparse module and have been
> having some trouble getting the help to format the way I want.

A quick perusal of the 'optparse.py' code shows me this:

========
[...]
class OptionParser([...]):
    def __init__([...],
                 formatter=None,
                 [...]):
        [...]
        if formatter is None:
            formatter = IndentedHelpFormatter()
        [...]
========

So, the OptionParser init method accepts the help formatter as the
'formatter' argument, defaulting to a new instance of
IndentedHelpFormatter.

Presumably, it's a matter of subclassing 'optparse.HelpFormatter' and
overriding the behaviour you want to change, then passing an instance
of your new class as the 'formatter' argument to the 'OptionParser()'
invocation.

-- 
 \        "Members of the general public commonly find copyright rules |
  `\     implausible, and simply disbelieve them."  -- Jessica Litman, |
_o__)                                              _Digital Copyright_ |
Ben Finney



More information about the Python-list mailing list