6.21.4.6 Callback example 3: check option order (generalized)

If you want to re-use this callback for several similar options (set a flag, but blow up if "-b" has already been seen), it needs a bit of work: the error message and the flag that it sets must be generalized.

def check_order(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)
[...]
parser.add_option("-a", action="callback", callback=check_order, dest='a')
parser.add_option("-b", action="store_true", dest="b")
parser.add_option("-c", action="callback", callback=check_order, dest='c')


Python Library Reference
Previous: Up: 6.21.4 Option Callbacks Next:

Release 2.4.4, documentation updated on 18 October 2006.
See About this document... for information on suggesting changes.

AltStyle によって変換されたページ (->オリジナル) /