469,362 Members | 2,546 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

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_option("-b", action="callback", callback=optionhandlr, 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(option, opt_str, value, parser):
if parser.values.b:
raise OptionValueError("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 1405
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_option("-b", action="callback", callback=optionhandlr, 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(option, opt_str, value, parser):
if parser.values.b:
raise OptionValueError("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(option, opt_str, value, parser):
if getattr(parser.values, option.dest):
msg = "can't use %s afer -%s" % (opt_str, option.dest)
raise OptionValueError, 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_option("-b", action="callback", callback=optionhandlr, 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Jean-Christian Imbeault | last post: by
1 post views Thread by vul | last post: by
2 posts views Thread by Brian Hirt | last post: by
7 posts views Thread by Antoine De Groote | last post: by
8 posts views Thread by Adil Akram | last post: by
5 posts views Thread by Steven W. Orr | last post: by
6 posts views Thread by Bill Cunningham | last post: by
3 posts views Thread by kimiraikkonen | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.