Re: [Fwd: Problem with OptionParser]

[email protected] (Berthold Höllmann) Wed, 25 Feb 2004 20:16:44 +0100
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
David Goodger <[email protected]> writes:

> Here's an interesting case, looks like a bug.

> From: Florian Lindner <[email protected]>
> Subject: Problem with OptionParser
> Newsgroups: gmane.comp.python.general
> Date: Wed, 25 Feb 2004 17:06:47 +0100
> Organization: T-Online
>
> Hello,
> I've this tiny Python sample:
>
> - - - -
> #!/usr/bin/python
>
> from optparse import OptionParser, OptionValueError
>
> def option_callback(option, opt, value, parser, *args, **kwargs):
>     pass
>
> optparser = OptionParser()
> optparser.add_option("-t", "--type", action="callback",
> callback=option_callback, type="string")
> (options, arguments) = optparser.parse_args()
> - - - -
>
The problems seems to be in 

    def _check_dest (self):
        if self.action in self.STORE_ACTIONS and self.dest is None:
            # No destination given, and we need one for this action.
            # Glean a destination from the first long option string,
            # or from the first short option string if no long options.
            if self._long_opts:
                # eg. "--foo-bar" -> "foo_bar"
                self.dest = self._long_opts[0][2:].replace('-', '_')
            else:
                self.dest = self._short_opts[0][1]

STORE_ACTIONS does not contain "callback", and surely should not?
Maybe the combination of "callback" and "type" should be regarded also
in _check_dest. The condition 

    def _check_dest (self):
        if (self.action in self.STORE_ACTIONS or (self.action == "callback" and self.type is not None)) and self.dest is None:

is working for me.

Regards

Berthold

-- 
[email protected] / http://starship.python.net/crew/bhoel/


-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click