optparse: store callback return value

S

sector119

Hi

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 optparse.py Option::take_action def and add there:

value = self.callback(self, opt, value, parser, *args, **kwargs) or
value
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?
 
W

wittempj

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
http://docs.python.org/lib/optparse-option-callbacks.html)

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",
callback=record_foo_seen)
-(options, args) = parser.parse_args()
-
-print options, args

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top