getopt not raising exception

Discussion in 'Python' started by Matthew Lear, Jan 10, 2010.

  1. Matthew Lear

    Matthew Lear Guest

    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
     
    Matthew Lear, Jan 10, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Guetttler
    Replies:
    1
    Views:
    597
    Thomas Guetttler
    Sep 10, 2003
  2. Calvin Spealman

    Ideas for yielding and exception raising

    Calvin Spealman, Jun 5, 2004, in forum: Python
    Replies:
    3
    Views:
    324
    Peter Hansen
    Jun 8, 2004
  3. SM
    Replies:
    1
    Views:
    335
    Duncan Booth
    Oct 21, 2004
  4. Antoon Pardon
    Replies:
    0
    Views:
    350
    Antoon Pardon
    Mar 15, 2005
  5. leo
    Replies:
    8
    Views:
    394
    Tom Anderson
    Oct 5, 2005
Loading...

Share This Page