pb with callback in the python optparse module

Posted by PierrOz on Stack Overflow See other posts from Stack Overflow or by PierrOz
Published on 2010-04-12T08:13:00Z Indexed on 2010/04/12 8:33 UTC
Read the original article Hit count: 382

Filed under:
|
|

Hi Guys,

I'm playing with Python 2.6 and its optparse module. I would like to convert one of my arguments to a datetime through a callback but it fails.

Here is the code:

def parsedate(option, opt_str, value, parser):
    option.date = datetime.strptime(value, "%Y/%m/%d")

def parse_options(args):
    parser = OptionParser(usage="%prog -l LOGFOLDER [-e]", version="%prog 1.0")
    parser.add_option("-d", "--date", action="callback", callback="parsedate", dest="date")  
    global options
    (options, args) = parser.parse_args(args)
    print option.date.strftime()

if __name__ == "__main__":
        parse_options(sys.argv[1:])

I get an error File: optparse.py in _check_callback "callback not callable". I guess I'm doing something wrong in the way I define my callback but what ? and why ? Can anyone help ?

© Stack Overflow or respective owner

Related posts about python

Related posts about optparse