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. Advertisements

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:
    697
    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:
    433
    Peter Hansen
    Jun 8, 2004
  3. SM
    Replies:
    1
    Views:
    403
    Duncan Booth
    Oct 21, 2004
  4. Antoon Pardon
    Replies:
    0
    Views:
    435
    Antoon Pardon
    Mar 15, 2005
  5. leo
    Replies:
    8
    Views:
    545
    Tom Anderson
    Oct 5, 2005
  6. Michael Goerz

    Raising exception on STDIN read

    Michael Goerz, Feb 27, 2008, in forum: Python
    Replies:
    14
    Views:
    699
    Ian Clark
    Feb 29, 2008
  7. Michael Hines
    Replies:
    1
    Views:
    271
    Arnaud Delobelle
    May 24, 2008
  8. Steven D'Aprano

    Modify an exception before re-raising it

    Steven D'Aprano, Mar 6, 2009, in forum: Python
    Replies:
    5
    Views:
    500
    Terry Reedy
    Mar 6, 2009
Loading...