optparse: store callback return value

Discussion in 'Python' started by sector119, Apr 24, 2005.

  1. sector119

    sector119 Guest

    Hi

    I use optparse with callback action, my callback function return some
    value, but optparse does not store this value, options.callback_dest
    always is None.

    How can I store callback function return value or callback option value
    like store action do?

    I modify optparse.py Option::take_action def and add there:

    value = self.callback(self, opt, value, parser, *args, **kwargs) or
    value
    setattr(values, dest, value)

    as for STORE_ACTIONS. but I do not like to do it like this :/ Why
    optparse does not do it, store return or option value, this way?
    sector119, Apr 24, 2005
    #1
    1. Advertising

  2. sector119

    Guest

    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@ubuntu:~$ ./test2.py --foo
    saw foo
    {} []
    martin@ubuntu:~$ ./test2.py
    {} []
    martin@ubuntu:~$
    , Apr 24, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Gittens
    Replies:
    1
    Views:
    444
    Peter Hansen
    Jul 10, 2005
  2. Steven W. Orr
    Replies:
    0
    Views:
    427
    Steven W. Orr
    Feb 9, 2007
  3. Steven W. Orr
    Replies:
    3
    Views:
    316
    Steven Bethard
    Feb 11, 2007
  4. Greenhorn
    Replies:
    15
    Views:
    792
    Keith Thompson
    Mar 6, 2005
  5. Stephen Kellett
    Replies:
    3
    Views:
    166
    gabriele renzi
    Dec 12, 2004
Loading...

Share This Page