14.3.4.7 Callback example 4: check arbitrary condition

Of course, you could put any condition in there--you're not limited to checking the values of already-defined options. For example, if you have options that should not be called when the moon is full, all you have to do is this:

def check_moon(option, opt_str, value, parser):
 if is_moon_full():
 raise OptionValueError("%s option invalid when moon is full"
 % opt_str)
 setattr(parser.values, option.dest, 1)
[...]
parser.add_option("--foo",
 action="callback", callback=check_moon, dest="foo")

(The definition of is_moon_full() is left as an exercise for the reader.)


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

Release 2.5.2, documentation updated on 21st February, 2008.
See About this document... for information on suggesting changes.

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