473,789 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retry:Question about optparse/OptionParser callback.

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_opti on("-b", action="callbac k", callback=option handlr, dest='b')

The Cookbook almost takes me there with a callback function that only
works for an option called b that takes no argument:

def optionhndlr(opt ion, opt_str, value, parser):
if parser.values.b :
raise OptionValueErro r("can't use %s after -b" % opt_str)
setattr(parser. values, option.dest, 1)

but warns that "it needs a bit of work: the error message and the flag
that it sets must be generalized". I do need to do my option processing in
an option processor with many options and I'd both like to do it in one
method (if possible) and learn a trick or two while I'm at it. Is it
possible to have a single callback that could be used in the general case?

All I need is to be taught how to fish...

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Feb 9 '07 #1
3 1523
Steven W. Orr wrote:
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_opti on("-b", action="callbac k", callback=option handlr, dest='b')

The Cookbook almost takes me there with a callback function that only
works for an option called b that takes no argument:

def optionhndlr(opt ion, opt_str, value, parser):
if parser.values.b :
raise OptionValueErro r("can't use %s after -b" % opt_str)
setattr(parser. values, option.dest, 1)

but warns that "it needs a bit of work: the error message and the flag
that it sets must be generalized". I do need to do my option processing
in an option processor with many options and I'd both like to do it in
one method (if possible) and learn a trick or two while I'm at it. Is it
possible to have a single callback that could be used in the general case?

All I need is to be taught how to fish...

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things
have .0.
happened but none stranger than this. Does your driver's license say
Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are
all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
If I understand your question, you are already half way there:

def optionhndlr(opt ion, opt_str, value, parser):
if getattr(parser. values, option.dest):
msg = "can't use %s afer -%s" % (opt_str, option.dest)
raise OptionValueErro r, msg
setattr(parser. values, option.dest, 1)

James
Feb 10 '07 #2
Steven W. Orr wrote:
I decided I could be more articulate. I hope this helps.
In case James did not guess right: try to provide more details about /what/
you want to achieve rather than /how/ you want to get there.
And please don't open a third thread for it.

Peter

Feb 10 '07 #3
Steven W. Orr wrote:
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_opti on("-b", action="callbac k", callback=option handlr, dest='b')
What do you want your callback to do? Actually, a better question is,
what do you want to happen when the user specifies "-b" at the command line?

STeVe
Feb 11 '07 #4

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

Similar topics

8
1508
by: Jean-Christian Imbeault | last post by:
I'm trying to convince another open-source project (phpOpenTracker) to modify their current INSERT sql queries. Currently they just do an INSERT into a table without first checking if their might be a record with the same primary key. The reason for this that they need fast inserts and most user I assume are using MySQL which silently drops INSERT queries that violate primary key constraints. But postgres on the other hand (and rightly...
1
1324
by: vul | last post by:
I created Windows Service in VB.Net with the name MSFaxListener. It should listen Windows Fax Service events and update SQL Server database when fax job changes it's status. In order to do that in OnStart event of MSFaxListener I'm trying to connect to MS Fax Service. I set Startup Type of MSFaxListener to Automatic, so it starts and listens. Sometimes it doesn't connect to MS Fax Service. I think it is because MS Fax Service did not start...
8
572
by: kurtcobain1978 | last post by:
-------------------------------------------------------------------------------- I need to do the exactly same thing in VB.NET. Load a unmanaged C DLL dynamically and then call a function in which I pass the callback function as an argument. My C function being called callback as type _cdecl. Does anybody have any ideas?
2
1297
by: Brian Hirt | last post by:
I have a question about locks. I have a stats table that get updated when some other table changes. Sometimes that other table is updated a 2nd time before the first stats update is finished which causes an error. I've tried using 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE' but get 'could not serialize access due to concurrent update' If i try 'READ COMMITED' i get primary key failures. This seems like it's a pretty common thing,...
7
5252
by: Antoine De Groote | last post by:
Hi, I hope I don't upset anybody by comparing Python to Ruby (again). Is there something like Ruby's retry keyword in Python? I couldn't find any thing... Regards, antoine
8
2031
by: Adil Akram | last post by:
There are situations where you want to retry/rerun the same code again (if user wants) for unlimited times for a particular exception thrown. For example in situations like there's no CD in the drive or some file is locked by another process you want to display message dialog to user with "Retry" and "Cancel" buttons (As Windows shows "Retry" option if no CD in drive) and if user clicks "Retry" you want to run the same code again that...
5
1809
by: Steven W. Orr | last post by:
I'm trying again, since no response indicates that I'm not providing enough info. I have module M1 which has the following line in it: StartTime = safe_dict_get ( dic, 'starttime', 0xFFFFFFFF ) It gets imported by modules M2 and M3. And finally, M4 imports both M2 and M3. So the idea is that in total we have 4 files called M1.py M2.py M3.py and M4.py
6
1797
by: Bill Cunningham | last post by:
As I have been studing my tutorial I came up with this question. I took char passw(char name); and initialied this function in a header with other includes for standard library headers. This doesn't seem to work so is what I want this? char passw(char *name); I haven't got to pointers in the tutorial and I'm taking my time. But in this particualr function should it be initialized with a pointer? Does
3
1371
by: kimiraikkonen | last post by:
Hi, It may a simple question but i needed to verify. I hava a for-next loop and when an exception occurs, i don't want my program to the jump out of try-catch block and i want it to retry the operation again. Here is what i meant: Try Dim x as integer
0
9666
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
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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
7529
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
6769
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
5418
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...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.