getopt

Discussion in 'Python' started by Don Low, Nov 15, 2003.

  1. Don Low

    Don Low Guest

    Hi,

    I'm going over a script that demonstrates the getopt function. I include
    the script here:


    #! /usr/bin/python

    import sys, getopt, string

    def help_message():
    print '''options.py -- uses getopt to recognize options
    Options: -h -- displays this help message
    -a -- expects an argument
    --file= -- expects an argument
    --view -- doesn't necessarily expect an argument
    --version -- displays Python version'''
    sys.exit(0)

    try:
    options, xarguments = getopt.getopt(sys.argv[1:], 'ha', \
    ['file=', 'view=', 'version', 'python-version'])
    except getopt.error:
    print '''Error: You tried to use an unknown option or the
    argument for an option that requires it was missing. Try
    `options.py -h\' for more information.'''
    sys.exit(0)

    for a in options[:]:
    if a[0] == '-h':
    help_message()

    for a in options[:]:
    if a[0] == '-a' and a[1] != '':
    print a[0]+' = '+a[1]
    options.remove(a)
    break
    elif a[0] == '-a' and a[1] == '':
    print '-a expects an argument'
    sys.exit(0)

    for a in options[:]:
    if a[0] == '--file' and a[1] != '':
    print a[0]+' = '+a[1]
    options.remove(a)
    break
    elif a[0] == '--file' and a[1] == '':
    print '--file expects an argument'
    sys.exit(0)

    for a in options[:]:
    if a[0] == '--view' and a[1] != '':
    print a[0]+' = '+a[1]
    options.remove(a)
    break
    elif a[0] == '--view' and a[1] == '':
    print '--view doesn\'t necessarily expect an argument...'
    options.remove(a)
    sys.exit(0)

    for a in options[:]:
    if a[0] == '--version':
    print 'options version 0.0.001'
    sys.exit(0)

    for a in options[:]:
    if a[0] == '--python-version':
    print 'Python '+sys.version
    sys.exit(0)

    # END OF SCRIPT

    When I execute the script with the -a option or the --view option as in:

    ../script_name -a myarg

    It should report back:

    -a = myarg

    Instead it gives me:

    -a expects an argument

    This goes the same for the --file argument. The only one that works as
    it should is the --view argument, as in:

    ../help2.py --view=myarg
    --view = ssdt

    This is because an equal sign (=) has been appended to 'view' when
    getopt is called. If I add an equal sign to file (file=), it starts
    working as it should too.

    Can anyone explain this?

    --
    Thanks,

    Don
    Don Low, Nov 15, 2003
    #1
    1. Advertising

  2. Don Low

    Peter Otten Guest

    Don Low wrote:

    > I'm going over a script that demonstrates the getopt function. I include
    > the script here:


    I you are only now exploring the possibilities of getopt, you might want to
    leapfrog and use the more powerful optparse module.

    > options, xarguments = getopt.getopt(sys.argv[1:], 'ha', \
    > ['file=', 'view=', 'version', 'python-version'])


    If you want the "a" option to require an argument, you have to append a
    colon serving the same purpose as the "=" for long options:

    options, xarguments = getopt.getopt(sys.argv[1:], 'ha:', \
    ['file=', 'view=', 'version', 'python-version'])


    Peter
    Peter Otten, Nov 15, 2003
    #2
    1. Advertising

  3. Don Low <> writes:

    > Hi,
    >
    > I'm going over a script that demonstrates the getopt function. I include
    > the script here:

    [...]
    > Can anyone explain this?


    Sorry for not answering your question, but can't you just use optparse (comes
    with python 2.3) or optik (same, but as downloadable library for older
    pythons)? It's much nicer.

    'as
    Alexander Schmolck, Nov 15, 2003
    #3
  4. Don Low wrote:

    > options, xarguments = getopt.getopt(sys.argv[1:], 'ha', \

    [snip]
    > When I execute the script with the -a option or the --view option as in:
    >
    > ./script_name -a myarg
    >
    > It should report back:
    >
    > -a = myarg
    >




    The second argument should have a colon after any letter/option that
    takes an argument.


    Also, a useful trick is to cast the opts to a dict. This prevents the
    need for iterating over the opts and allows for concise definition of
    default arguments.

    e.g.


    opts,args = getopt.getopt(sys.argv[1:] , 'h:a:v' )
    opts = dict(opts)

    hparm = opts.get('-h','cutie')
    aparm = float( opts.get( '-a', math.pi ) )
    verbose = opts.has_key('-v')


    Caveats: the dict trick does not allow an option to be specified more
    than once (e.g. -vv), it also loses ordering.


    - Mark
    Mark Borgerding, Nov 18, 2003
    #4
    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. Arvind Kumar

    Problem with Getopt

    Arvind Kumar, Nov 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    569
    Jim Gibson
    Nov 3, 2003
  2. dima
    Replies:
    9
    Views:
    699
    llewelly
    Sep 5, 2003
  3. Ramiro Rodriguez

    getopt

    Ramiro Rodriguez, Oct 14, 2003, in forum: C++
    Replies:
    6
    Views:
    4,002
    Lorenzo Bettini
    Oct 17, 2003
  4. Jem Berkes

    Re: Dealing with fragmented getopt parameters

    Jem Berkes, Oct 30, 2003, in forum: C Programming
    Replies:
    2
    Views:
    334
    Jem Berkes
    Oct 30, 2003
  5. Spartan815

    getopt

    Spartan815, Jan 25, 2004, in forum: C Programming
    Replies:
    2
    Views:
    1,073
    Peter Nilsson
    Jan 26, 2004
Loading...

Share This Page