This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jkankiewicz
Recipients georg.brandl, jkankiewicz
Date 2009-01-04.06:50:40
SpamBayes Score 0.0037940375
Marked as misclassified No
Message-id <1231051843.8.0.720726802561.issue4827@psf.upfronthosting.co.za>
In-reply-to
Content
"Callback example 1: trivial callback" reads

    Here’s an example of a callback option that takes no arguments, and
simply records that the option was seen:

        def record_foo_seen(option, opt_str, value, parser):
            parser.saw_foo = True

        parser.add_option("--foo", action="callback",
callback=record_foo_seen)

but the following paragraph

    Of course, you could do that with the store_true action.

is wrong because

        parser.add_option("--foo", action="store_true", dest="saw_foo")

would actually be duplicated by

        def record_foo_seen(option, opt_str, value, parser):
            parser.values.saw_foo = True

        parser.add_option("--foo", action="callback",
callback=record_foo_seen)

For example:
>>> from optparse import OptionParser
>>> parser = OptionParser()
>>> def record_foo_seen(option, opt_str, value, parser):
...     parser.saw_foo = True
...
>>> parser.add_option("--foo", action="callback", callback=record_foo_seen)
<Option at 0xab4f58: --foo>
>>> parser.parse_args(['--foo'])
(<Values at 0xabb0f8: {}>, [])
>>> parser = OptionParser()
>>> parser.add_option("--foo", action="store_true", dest="saw_foo")
<Option at 0xabb1e8: --foo>
>>> parser.parse_args(['--foo'])
(<Values at 0xabb1c0: {'saw_foo': True}>, [])
>>> parser = OptionParser()
>>> def record_foo_seen(option, opt_str, value, parser):
...     parser.values.saw_foo = True
...
>>> parser.add_option("--foo", action="callback", callback=record_foo_seen)
<Option at 0xabb508: --foo>
>>> parser.parse_args(['--foo'])
(<Values at 0xabb3f0: {'saw_foo': True}>, [])
History
Date User Action Args
2009-01-04 06:50:43jkankiewiczsetrecipients: + jkankiewicz, georg.brandl
2009-01-04 06:50:43jkankiewiczsetmessageid: <1231051843.8.0.720726802561.issue4827@psf.upfronthosting.co.za>
2009-01-04 06:50:42jkankiewiczlinkissue4827 messages
2009-01-04 06:50:41jkankiewiczcreate