[New-bugs-announce] [issue28171] getopt.getopt error processing long_options

Mike Hagerty report at bugs.python.org
Thu Sep 15 14:56:36 EDT 2016

New submission from Mike Hagerty:

Here's the relevant code:

opts, args = getopt.getopt(sys.argv[1:], "ih", ["help", "f1Hz","startdate=", "ndays="])

>main.py -i --f1H --startdat=2016-08-22 --ndays 2

Here's what getopt returns into opts:
opts= [('-i', ''), ('--f1Hz', ''), ('--startdate', '2016-08-22'), ('--ndays', '2')]

As you can see, I gave incomplete long_option names ("--f1H" instead of "--f1Hz",
"startdat" instead of "startdate") but getopt doesn't care.

getopt appears to scan just until the end of the input flag ("--f1H") and replaces this in opts with the full flag name passed to it ("--f1Hz") that matches the first characters.  

This means, for instance, you couldn't do:
>main.py --flag1=something --flag11=something_else

Surely this isn't intended behavior (?)

messages: 276588
nosy: hagertmb at bc.edu
priority: normal
severity: normal
status: open
title: getopt.getopt error processing long_options
type: behavior
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list