This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Getopt overwrites variables unexpectedly
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Jak, r.david.murray
Priority: normal Keywords:

Created on 2015-07-03 09:32 by Jak, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg246153 - (view) Author: Jak (Jak) Date: 2015-07-03 09:32
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.
msg246181 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-07-03 14:33
This behavior is correct:

rdmurray@session:~>getopt a:b:c -a value1 -b value2 -cbanana
 -a value1 -b value2 -c -b anana --
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68744
2015-07-03 14:33:46r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg246181

resolution: not a bug
stage: resolved
2015-07-03 09:32:15Jakcreate