Search code examples
pythongetopt

Python getopt behavior


#!/usr/bin/python
import sys
import getopt

o, a = getopt.getopt(sys.argv[1:], 'ab:c:')

print "options: %s" % (o)
print "arguments: %s" % (a)

running python:

python TestOpt.py a b 10 c 20

it prints out like this:

options: []
arguments: ['a', 'b', '10', 'c', '20']

I don't understand why options is an empty list while arguments looks like options?


Solution

  • Try it with:

    python TestOpt.py -a -b 10 -c 20 foobar