classification
Title: getopt should not accept no_argument that ends with '='
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.1, Python 3.0, Python 2.7, Python 2.6
process
Status: open Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, haypo, wangchun (3)
Priority: Keywords patch

Created on 2008-12-11 06:54 by wangchun, last changed 2009-03-27 17:04 by amaury.forgeotdarc.

Files
File name Uploaded Description Edit Remove
getopt.py.diff wangchun, 2008-12-11 07:12
getopt-2.patch haypo, 2009-03-27 01:28
Messages (4)
msg77597 - (view) Author: Wang Chun (wangchun) Date: 2008-12-11 06:54
Consider the following program tmp.py:

import sys, getopt
print(getopt.getopt(sys.argv[1:], '', ['help']))

The program accept "--help" without a value:

python helloworld.py --help

But if someone invoke the program like:

python helloworld.py --help=

Python should raise an error.

"--help=" is not considered as no_argument in libc's getopt implementation (tested on Mac OS X 
Leopard):

#include <getopt.h>

static struct option longopts[] = { 
        { "help", no_argument, NULL, "h" },
};

#include <getopt.h>

static struct option longopts[] = { 
        { "help", no_argument, NULL, 'h' },
};

int main(int argc, char **argv)
{
        while (getopt_long(argc, argv, "h", longopts, NULL) != -1);
        return 0;
}

macbook:~/tmp$ gcc -o tmp tmp.c
macbook:~/tmp$ ./tmp --help=
tmp: option `--help' doesn't allow an argument
macbook:~/tmp$
msg78283 - (view) Author: STINNER Victor (haypo) Date: 2008-12-25 23:20
Can you please also write a regression test in 
Lib/test/test_getopt.py?
msg84247 - (view) Author: STINNER Victor (haypo) Date: 2009-03-27 01:28
Updated patch with a regression test.
msg84276 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) Date: 2009-03-27 17:04
The patch is good.
History
Date User Action Args
2009-03-27 17:04:10amaury.forgeotdarcsetresolution: accepted

messages: + msg84276
nosy: + amaury.forgeotdarc
2009-03-27 01:28:56hayposetfiles: + getopt-2.patch

messages: + msg84247
2008-12-25 23:20:10hayposetnosy: + haypo
messages: + msg78283
2008-12-20 14:28:46loewissetversions: - Python 2.5, Python 2.4, Python 2.3, Python 2.2.3, Python 2.2.2, Python 2.2.1, Python 2.2, Python 2.1.2, Python 2.1.1, Python 2.5.3
2008-12-11 07:12:45wangchunsetfiles: + getopt.py.diff
keywords: + patch
2008-12-11 06:54:17wangchuncreate