Skip to content

Commit eccc5fa

Browse files
author
Victor Stinner
committed
Issue #4629: getopt raises an error if an argument ends with = whereas getopt
doesn't except a value (eg. --help= is rejected if getopt uses ['help='] long options).
1 parent a565874 commit eccc5fa

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/getopt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def do_longs(opts, opt, longopts, args):
156156
if not args:
157157
raise GetoptError('option --%s requires argument' % opt, opt)
158158
optarg, args = args[0], args[1:]
159-
elif optarg:
159+
elif optarg is not None:
160160
raise GetoptError('option --%s must not have an argument' % opt, opt)
161161
opts.append(('--' + opt, optarg or ''))
162162
return opts, args

Lib/test/test_getopt.py

+6
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ def test_libref_examples(self):
173173
m = types.ModuleType("libreftest", s)
174174
run_doctest(m, verbose)
175175

176+
def test_issue4629(self):
177+
longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
178+
self.assertEquals(longopts, [('--help', '')])
179+
longopts, shortopts = getopt.getopt(['--help=x'], '', ['help='])
180+
self.assertEquals(longopts, [('--help', 'x')])
181+
self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help'])
176182

177183
def test_main():
178184
run_unittest(GetoptTests)

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@ C-API
473473
Library
474474
-------
475475

476+
- Issue #4629: getopt raises an error if an argument ends with = whereas getopt
477+
doesn't except a value (eg. --help= is rejected if getopt uses ['help='] long
478+
options).
479+
476480
- Issue #7989: Added pure python implementation of the `datetime`
477481
module. The C module is renamed to `_datetime` and if available,
478482
overrides all classes defined in datetime with fast C impementation.

0 commit comments

Comments
 (0)