Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > optparse: store callback return value

Thread Tools

optparse: store callback return value

Posts: n/a

I use optparse with callback action, my callback function return some
value, but optparse does not store this value, options.callback_dest
always is None.

How can I store callback function return value or callback option value
like store action do?

I modify Option::take_action def and add there:

value = self.callback(self, opt, value, parser, *args, **kwargs) or
setattr(values, dest, value)

as for STORE_ACTIONS. but I do not like to do it like this :/ Why
optparse does not do it, store return or option value, this way?

Reply With Quote
Posts: n/a
Callbacks are functions called when an optparse.OptionParser() object
has a callback option defined (don't know how to say this less obvious
sounding...) (they are documented in

Example (based on an example in the documentation):

this script:
-#!/usr/bin/env python
-import optparse
-def record_foo_seen(option, opt_str, value, parser):
- print 'saw foo'
-parser = optparse.OptionParser()
-parser.add_option("--foo", action="callback",
-(options, args) = parser.parse_args()
-print options, args

prints when executed with or without argument:
martin@ubuntu:~$ ./ --foo
saw foo
{} []
martin@ubuntu:~$ ./
{} []

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to suppress "DeprecationWarning: Old style callback, use cb_func(ok,store) instead" John Nagle Python 7 02-06-2007 08:52 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
How can I get the return value from a Ruby function, but inside the set_trace_func callback? Stephen Kellett Ruby 3 12-12-2004 11:25 AM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM