getopt not raising exception

M

Matthew Lear

Hello,

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

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

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?

Thanks,
-- Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top