Retry:Question about optparse/OptionParser callback.

Steven W. Orr steveo at syslang.net
Fri Feb 9 16:04:16 EST 2007


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



More information about the Python-list mailing list