[Python-Dev] Small additions to optparser

Gustavo Niemeyer niemeyer at conectiva.com
Thu Apr 29 15:47:35 EDT 2004


Hi Folks, 

I have two small customizations I've used in most projects I've
plugged optparse into. Since they're simple and generic, I'd
like to integrate them back in optparser. These features are:

- Add a help formatter which uses capitalized headers.
- Add a "help" keyword in OptionParser, allowing straightforward
  addition of a custom help messages.

Comments?

Also, how are we currently handling changes in optparser? Do we
have an "official" maintainer (Greg?). IOW, should I:

- Send a proposed patch to SourceForge
- Contact the original project
- Commit the patch


FWIW, here is the proposed functionality:


class CapitalizedHelpFormatter(optparse.IndentedHelpFormatter):

    def format_usage(self, usage):
        return optparse.IndentedHelpFormatter \
                .format_usage(self, usage).capitalize()

    def format_heading(self, heading):
        return optparse.IndentedHelpFormatter \
                .format_heading(self, heading).capitalize()

class OptionParser(optparse.OptionParser):

    def __init__(self, *args, **kwargs):
        optparse.OptionParser.__init__(self, *args, **kwargs)
        self._override_help = kwargs.get("help")

    def format_help(self, formatter=None):
        if self._override_help:
            return self._override_help
        else:
            return optparse.OptionParser.format_help(self, formatter)

-- 
Gustavo Niemeyer
http://niemeyer.net



More information about the Python-Dev mailing list