[Python-bugs-list] [Bug #113463] Inconsistent handling of arg -/-- by getopt.getopt() ?

noreply@sourceforge.net noreply@sourceforge.net
Sat, 2 Sep 2000 19:44:38 -0700


Bug #113463, was updated on 2000-Sep-02 19:44
Here is a current snapshot of the bug.

Project: Python
Category: Modules
Status: Open
Resolution: None
Bug Group: None
Priority: 5
Summary: Inconsistent handling of arg -/-- by getopt.getopt() ?

Details: Problem with function getopt of module getopt.py of Python 1.5.1?

Case 1:

>>> import getopt, string
>>> args = string.split('-a -b b_val - some more stuff -c')
>>> optlist, trailing_args = getopt.getopt(args, 'ab:c')
>>> optlist
[('-a', ''), ('-b', 'b_val')]
>>> trailing_args
['-', 'some', 'more', 'stuff', '-c']

Case 2:

>>> import getopt, string
>>> args = string.split('-a -b b_val -- some more stuff -c')
>>> optlist, trailing_args = getopt.getopt(args, 'ab:c')
>>> optlist
[('-a', ''), ('-b', 'b_val')]
>>> trailing_args
['some', 'more', 'stuff', '-c']

The differences are: 
In case 1, I have "-" in the args list and I get it as the first element of the trailing_args output list; In case 2, I have "--" in the args list and I get nothing about it in the trailing_args output list.

This happens because in line 7 of the function getopt (reproduced below) the "--" element of args is discarded.

~~~~~~~~~
Function getopt:

def getopt(args, shortopts, longopts = []):
    list = []
    longopts = longopts[:]
    longopts.sort()
    while args and args[0][:1] == '-' and args[0] != '-':
        if args[0] == '--':
            args = args[1:]
            break
        if args[0][:2] == '--':
            list, args = do_longs(list, args[0][2:], longopts, args[1:])
        else:
            list, args = do_shorts(list, args[0][1:], shortopts, args[1:])

    return list, args


For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=113463&group_id=5470