[Python-Dev] Re: Call for reviewer!

David Goodger dgoodger@bigfoot.com
Tue, 15 Aug 2000 22:36:02 -0400


I thought the "backwards compatibility" issue might be a sticking point ;>
And I *can* see why.

So, if I were to rework the patch to remove the incompatibility, would it
fly or still be shot down? Here's the change, in a nutshell:

Added a function getoptdict(), which returns the same data as getopt(), but
instead of a list of [(option, optarg)], it returns a dictionary of
{option:optarg}, enabling random/direct access.

getoptdict() turns this:

    if __name__ == '__main__':
        import getopt
        opts, args = getopt.getopt(sys.argv[1:], 'a:b')
        if len(args) <> 2:
            raise getopt.error, 'Exactly two arguments required.'
        options = {'a': [], 'b': 0}  # default option values
        for opt, optarg in opts:
            if opt == '-a':
                options['a'].append(optarg)
            elif opt == '-b':
                options['b'] = 1
        main(args, options)

into this:

    if __name__ == '__main__':
        import getopt
        opts, args = getopt.getoptdict(sys.argv[1:], 'a:b',
                                       repeatedopts=APPEND)
        if len(args) <> 2:
            raise getopt.error, 'Exactly two arguments required.'
        options = {'a': opts.get('-a', [])}
        options['b'] = opts.has_key('-b')
        main(args, options)

(Notice how the defaults get subsumed into the option processing, which goes
from 6 lines to 2 for this short example. A much higher-level interface,
IMHO.)

BUT WAIT, THERE'S MORE! As part of the deal, you get a free test_getopt.py
regression test module! Act now; vote +1! (Actually, you'll get that no
matter what you vote. I'll remove the getoptdict-specific stuff and resubmit
it if this patch is rejected.)

The incompatibility was introduced because the current getopt() returns an
empty string as the optarg (second element of the tuple) for an argumentless
option. I changed it to return None. Otherwise, it's impossible to
differentiate between an argumentless option '-a' and an empty string
argument '-a ""'. But I could rework it to remove the incompatibility.

Again: If the patch were to become 100% backwards-compatible, with just the
addition of getoptdict(), would it still be rejected, or does it have a
chance?

Eagerly awaiting your judgement...

-- 
David Goodger    dgoodger@bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)