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

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0

    getOpt confused >_<


    Okay, after reading some tutorials in getOpt, I'm still confused of how exactly it works.

    I learnt that this line is quite important for getOpt.

    opts, args = getopt.getopt(sys.argv[1:], "ab:")

    However, even after reading some tutorial, I still don't understand how this line work...I know that it will assign 2 "things", one in opts, and the other in args. When i try to print it out, those "things" are "list" I think.

    So, how does that line can help for manipulating additional command arguments? like python filename.py -a blablab -b blab -c etc.etc. etc...

    Please clarify and show me step by step.

    Thank you.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The getopt() function returns a Tuple of two Lists which hold the options and anything else.

    http://www.python.org/doc/2.4/tut/node7.html#SECTION007300000000000000000

    A look at the functions and how it's called:

    optList, argList = getopt.getopt(arguments, 'valid:')
    Here:

    *optList will contain a list of option-value pairs i.e. ('-a', 'value').
    *argList will hold anything else arguments specified without options.
    *arguments is the list you want to work with, usually sys.argv[1:].
    *valid is a string containing the valid option letters followed by a colon.

    Note: there is also another, option argument for handling long options.

    There are several good examples in the getopt modules docs (below) however here are a few more:

    http://www.python.org/doc/2.4/lib/module-getopt.html

    Code:
    >>> args = '-ab -c -d python'.split()
    >>> args
    ['-ab', '-c', '-d', 'python']
    >>> 
    >>> getopt.getopt(args, 'abcd')
    ([('-a', ''), ('-b', ''), ('-c', ''), ('-d', '')], ['python'])
    >>> 
    >>> optionList, argumentList = getopt.getopt(args, 'abcd')
    >>> optionList
    [('-a', ''), ('-b', ''), ('-c', ''), ('-d', '')]
    >>> argumentList
    ['python']
    >>> 
    >>> optionList, argumentList = getopt.getopt(args, 'abcd:')
    >>> optionList
    [('-a', ''), ('-b', ''), ('-c', ''), ('-d', 'python')]
    >>> argumentList
    []
    >>>
    I would suggest that you fire up your Python shell and give it a try. Change the options and see what you get .

    Please try to avoid double posting victorti.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0
    Thx a lot

    Code:
     optList, argList = getopt.getopt(arguments, 'valid:')
    But there is still something that I'm not sure. So you are saying that optList contains option argument and arglist contains non-option argument.

    So that mean, if we have:

    python test.py hellodude -a -b -c cool

    Lets say the "valid" is 'abc:' in this one, -a, -b and -c will go to optList, since they are "option" argument. and hellodude and cool will go to arglist? am i right?

    However, I don't understand what is the meaning/difference of putting the colon...:

    optionList, argumentList = getopt.getopt(args, 'abcd:')
    or not putting the colon
    optionList, argumentList = getopt.getopt(args, 'abcd')

    if we put : , it will considered the non option argument as optList's value? and argList will be empty all the time when we put colon?? Am I right?
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Options come first on the command line so everything after the first non-option is considered a non-option. This means that -a -b -c have to come before hellodude.

    The colon appears to tell getopt weather or not the option can accept a value i.e. -a -b example.

    Consider this example:

    Code:
    >>> import sys, getopt
    >>> 
    >>> sys.argv = ['test.py', 'hellodude', '-a', '-b', '-c', 'cool']
    >>> sys.argv
    ['test.py', 'hellodude', '-a', '-b', '-c', 'cool']
    >>> 
    >>> optionList, argumentList = getopt.getopt(sys.argv[1:], 'abc')
    >>> optionList
    []
    >>> argumentList
    ['hellodude', '-a', '-b', '-c', 'cool']
    >>> 
    >>> sys.argv = ['test.py', '-a', '-b', '-c', 'cool', 'hellodude']
    >>> sys.argv
    ['test.py', '-a', '-b', '-c', 'cool', 'hellodude']
    >>> 
    >>> optionList, argumentList = getopt.getopt(sys.argv[1:], 'abc')
    >>> optionList
    [('-a', ''), ('-b', ''), ('-c', '')]
    >>> argumentList
    ['cool', 'hellodude']
    >>> 
    >>> optionList, argumentList = getopt.getopt(sys.argv[1:], 'abc:')
    >>> optionList
    [('-a', ''), ('-b', ''), ('-c', 'cool')]
    >>> argumentList
    ['hellodude']
    >>>
    As you can see, when the colon is provided 'cool' becomes the value of the -c option; without it cool treated is a non-option.

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0
    After some try, I encounter another problem with getopt:

    Here is the problem:

    Code:
    >>> sys.argv = ['test.py', '-a', '-b', 'hellodude', '-c']
    >>> sys.argv
    ['test.py', '-a', '-b', 'hellodude', '-c']
    >>> opts, args = getopt.getopt(sys.argv[1:], 'abc')
    >>> opts
    [('-a', ''), ('-b', '')]
    >>> args
    ['hellodude', '-c']
    >>>
    But what I want is that opts should have:
    [('-a', ''), ('-b', 'hellodude'), ('-a', "")]
    and args is empty.

    Hope you can help me.

IMN logo majestic logo threadwatch logo seochat tools logo