Search code examples
pythonpython-2.7optparse

ValueError in optparse when using type


I've been given a script that uses optparse. I'm not familiar with this module, so I've been reading up on it and trying various simple exercises to get a better understanding of how it works. The code below is giving me a ValueError, telling me 'int' is not callable. It runs fine if I don't use the type option. Is this correct, or am I missing something?

import argparse

parser = argparse.ArgumentParser(description='Non-optional')

parser.add_argument('count', action='store', type="int")
parser.add_argument('units', action='store')

print parser.parse_args()

Called from command line as: python.exe module1.py 3, Test


Solution

  • The type parameter has to be an actual type, not the name of a type.

    parser.add_argument('count', action='store', type=int)
    

    Note that I removed the " around int.