optparse: store callback return value

wittempj@hotmail.com martin.witte at gmail.com
Sun Apr 24 12:45:02 EDT 2005


Callbacks are functions called when an optparse.OptionParser() object
has a callback option defined (don't know how to say this less obvious
sounding...) (they are documented in
http://docs.python.org/lib/optparse-option-callbacks.html)

Example (based on an example in the documentation):

this script:
-#!/usr/bin/env python
-import optparse
-def record_foo_seen(option, opt_str, value, parser):
-    print 'saw foo'
-
-parser = optparse.OptionParser()
-parser.add_option("--foo", action="callback",
callback=record_foo_seen)
-(options, args) = parser.parse_args()
-
-print options, args

prints when executed with or without argument:
martin at ubuntu:~$ ./test2.py --foo
saw foo
{} []
martin at ubuntu:~$ ./test2.py
{} []
martin at ubuntu:~$




More information about the Python-list mailing list