[New-bugs-announce] [issue24556] Getopt overwrites variables unexpectedly

Jak report at bugs.python.org
Fri Jul 3 11:32:15 CEST 2015

New submission from Jak:

The getopt library has, what I assume is, some unexpected behaviour when adding extra text to command line parameter that getopt expects as a flag.

Using input parameters a, b and c as an example below, where a and b both take values and c is a flag.

Example code:
options, remainders = getopt.getopt(sys.argv[1:], "a:b:c")

Normal output is given when you supply sensible values for a, b and c:

Input: -a value1 -b value2 -c
Output: [('-a', 'value1'), ('-b', 'value2'), ('-c', '')]

Unexpected output happens when you give extra text after the '-c' that begins with a letter matching that of a previous parameter:

Input -a value1 -b value2 -cbanana
Output: [('-a', 'value1'), ('-b', 'value2'), ('-c', ''), ('-b', 'anana')]

Looping through the output variables, as most example programs do, results in the value for '-b' being over-written.

components: Library (Lib)
messages: 246153
nosy: Jak
priority: normal
severity: normal
status: open
title: Getopt overwrites variables unexpectedly
type: behavior
versions: Python 2.7, Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list