optparse: store callback return value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sector119

    #1

    optparse: store callback return value

    Hi

    I use optparse with callback action, my callback function return some
    value, but optparse does not store this value, options.callbac k_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_ac tion def and add there:

    value = self.callback(s elf, 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?

  • wittempj@hotmail.com

    #2
    Re: optparse: store callback return value

    Callbacks are functions called when an optparse.Option Parser() 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-...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.Option Parser()
    -parser.add_opti on("--foo", action="callbac k",
    callback=record _foo_seen)
    -(options, args) = parser.parse_ar gs()
    -
    -print options, args

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

    Comment

    Working...