473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

optparse: store callback return value

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?

Jul 19 '05 #1
1 2834
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-...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:~$

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: washu | last post by:
Hi, I'm was going through the module help located on http://docs.python.org/lib/optparse-store-action.html and I tried modifying it a tiny bit and things don't work. If someone could tell me...
1
by: Eric O. Angell | last post by:
Say I have class Header: def __init__(self): self.foo = 0 # ... more fields and I have some commandline options that can specify better values for things in the header, such as foo. If I try...
6
by: ashtonn | last post by:
Hello, How do i make an option passed through command line, using optparse global. I need to import this value in another file. This is what iam trying to do. $python test.py -d...
3
by: Steven W. Orr | last post by:
I decided I could be more articulate. I hope this helps. I'm writing a program that needs to process options. Due to the nature of the program with its large number of commandline options, I...
0
by: Robert Kern | last post by:
Jeff Keasler wrote: If you code it up with unit tests and documentation, it has a good chance. But in the meantime, you can tell optparse to stop processing options using the standard "--"...
7
by: wannymahoots | last post by:
optparse seems to be escaping control characters that I pass as arguments on the command line. Is this a bug? Am I missing something? Can this be prevented, or worked around? This behaviour...
10
by: James | last post by:
Hi, I would like to know your thoughts on a proposed change to optparse that I have planned. It is possible to add default values to multiple options using the set_defaults. However, when adding...
2
by: hofer | last post by:
Hi, I get following warning with a python script: optparse.py:668: FutureWarning: %u/%o/%x/%X of negative int will return a signed string in Python 2.4 and up my code:
0
by: John O'Hagan | last post by:
Here's a strange one for you: I have a generator function which produces lists of numbers and takes options which influence the output. The generator contains a loop, and to enable the options...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.