getopt not raising exception

Matthew Lear matt at
Sun Jan 10 12:30:24 CET 2010


I'm having problems getting getopt to function correctly. Basically, no
exception is being raised if no argument is passed to the code snippet
below. I've read the Python documentation and tried example code from
various sources which should cause an exception, only they don't. I've
also tried executing the code on different machines too but to no avail.
I'm sure I'm obviously doing something wrong but can't determine what.
Any help would be much appreciated indeed.

import sys, getopt

        opts, args = getopt.getopt(sys.argv, "h:", ["help"])
except getopt.GetoptError:
        print "error"

If no args are passed when the script is run there is no exception
raised. Why? Surely the "h:" means that this option must be passed?

--  Matt

More information about the Python-list mailing list