[New-bugs-announce] [issue29681] getopt fails to handle option with missing value in middle of list

Marshall Giguere report at bugs.python.org
Tue Feb 28 22:32:30 EST 2017


New submission from Marshall Giguere:

Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
>>> from getopt import getopt
>>> argv = [ '-a', '-b', '-c', '-d', 'foo']
>>> opts, args = getopt( argv, 'abc:d:')
>>> opts
[('-a', ''), ('-b', ''), ('-c', '-d')]
>>> args
['foo']

Expected behavior:
opts = [('-a', ''), ('-b', ''), ('-c', ''), ('-d', 'foo')]
Throws execption:
getopt.GetoptError: option -c requires argument

Note that -c requires a value, getopt swallows the next option '-d' as the argument to -c.  However, if -c is the last option on the command line getopt properly throws an execption "getopt.GetoptError: option -c requires argument".

The documentation states that getopt will throw an exception when an option requiring a value is missing and exception will be thrown.
"exception getopt.GetoptError
This is raised when an unrecognized option is found in the argument list or when an option requiring an argument is given none."

The option -c requires an argument, none was given, no exception is thrown.  Instead the next option, '-d', is taken as the argument.  I have also tried this test on 2.7 with the same result.

----------
components: Extension Modules
messages: 288731
nosy: Marshall Giguere
priority: normal
severity: normal
status: open
title: getopt fails to handle option with missing value in middle of list
type: behavior
versions: Python 2.7, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29681>
_______________________________________


More information about the New-bugs-announce mailing list