Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optparse: Callback example 1 is confusing #49077

Closed
jkankiewicz mannequin opened this issue Jan 4, 2009 · 2 comments
Closed

optparse: Callback example 1 is confusing #49077

jkankiewicz mannequin opened this issue Jan 4, 2009 · 2 comments
Assignees
Labels
docs Documentation in the Doc dir

Comments

@jkankiewicz
Copy link
Mannequin

jkankiewicz mannequin commented Jan 4, 2009

BPO 4827
Nosy @birkenfeld

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/birkenfeld'
closed_at = <Date 2009-02-05.11:33:36.178>
created_at = <Date 2009-01-04.06:50:42.750>
labels = ['docs']
title = 'optparse: Callback example 1 is confusing'
updated_at = <Date 2009-02-05.11:33:36.177>
user = 'https://bugs.python.org/jkankiewicz'

bugs.python.org fields:

activity = <Date 2009-02-05.11:33:36.177>
actor = 'georg.brandl'
assignee = 'georg.brandl'
closed = True
closed_date = <Date 2009-02-05.11:33:36.178>
closer = 'georg.brandl'
components = ['Documentation']
creation = <Date 2009-01-04.06:50:42.750>
creator = 'jkankiewicz'
dependencies = []
files = []
hgrepos = []
issue_num = 4827
keywords = []
message_count = 2.0
messages = ['79039', '81206']
nosy_count = 2.0
nosy_names = ['georg.brandl', 'jkankiewicz']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue4827'
versions = ['Python 2.6', 'Python 2.5', 'Python 2.4', 'Python 3.0', 'Python 3.1', 'Python 2.7']

@jkankiewicz
Copy link
Mannequin Author

jkankiewicz mannequin commented Jan 4, 2009

"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}>, [])

@jkankiewicz jkankiewicz mannequin assigned birkenfeld Jan 4, 2009
@jkankiewicz jkankiewicz mannequin added the docs Documentation in the Doc dir label Jan 4, 2009
@birkenfeld
Copy link
Member

You're right, I've fixed that in r69298.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

1 participant