1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Rep Power

    need help with getopt

    i can't seem to understand how to get it to work properly

    let's say i have a program program.py that takes 3 options


    now t has to take an argument while the arguments for q and s are optional... does it go something like this?

    opts, args = getopt.getopt(sys.argv[1:], "t:qs")
    except getopt.GetoptError:
    for opt, arg in opts:
    if opt == '-t':
    #do something
    if opt == '-q':
    #do something
    if opt == '-s':
    #do something
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    London, England
    Rep Power
    As far as I can see, your code is correct. You say you are you having problems getting it working - what is it doing (or not doing)?

    A couple of suggestions:

    1) if you wrap your code in the post with [CODE] tags then it will be easier for the rest of us to read.

    2) You use the name 'args' for the non-option parameters returned from the getopt function, and 'arg' for the value of the option. These could be easily confused, since they are quite different things. I would suggest renaming 'arg' to 'value' or 'optionValue' or something similar.

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo