473,803 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.callbac k_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_ac tion def and add there:

value = self.callback(s elf, 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 2864
Callbacks are functions called when an optparse.Option Parser() 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.Option Parser()
-parser.add_opti on("--foo", action="callbac k",
callback=record _foo_seen)
-(options, args) = parser.parse_ar gs()
-
-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
3168
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 what I'm doing wrong, I'd appreciate it. The script that works (based on the code on the webpage) is: #!/usr/bin/env python
1
2019
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 the obvious (to me): parser = optparse.OptionParser() parser.add_option('--secret', type='string', dest='header', default=Header())
6
2637
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 ffffffffffff i should be able to store 'ffffffffffff' as global so that i could
3
1526
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 would like to write a callback to be set inside add_option. Something like this: parser.add_option("-b", action="callback", callback=optionhandlr, dest='b')
0
928
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 "--" marker. For example: $ cat mycommand.py import optparse parser = optparse.OptionParser()
7
4120
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 doesn't occur with non-control characters. For example, if this program (called test.py): from optparse import OptionParser parser = OptionParser() parser.add_option("-d", dest="delimiter", action="store")
10
1433
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 descriptions to options the developer has to specify it in each add_option() call. This results in unreadable code such as: parser.add_option('-q', '--quiet' , action="store_false", dest='verbose',
2
1252
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
1281
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 to have a different value on each iteration, the options may themselves be instances of the same generator, in the form of attributes of an optparse object. In the body of the loop, the next() method is called on each generator to get the new value...
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.