Skip to content

Commit eda5583

Browse files
committed
Mark getopt error messages as localizable (fixes #11371).
Patch by Filip Gruszczyński.
1 parent 16e6f4c commit eda5583

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Lib/getopt.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions
2020
# to class-based exceptions.
2121
#
22-
# Peter Åstrand <astrand@lysator.liu.se> added gnu_getopt().
22+
# Peter Åstrand <astrand@lysator.liu.se> added gnu_getopt().
2323
#
2424
# TODO for gnu_getopt():
2525
#
@@ -34,6 +34,7 @@
3434
__all__ = ["GetoptError","error","getopt","gnu_getopt"]
3535

3636
import os
37+
from gettext import gettext as _
3738

3839
class GetoptError(Exception):
3940
opt = ''
@@ -153,10 +154,10 @@ def do_longs(opts, opt, longopts, args):
153154
if has_arg:
154155
if optarg is None:
155156
if not args:
156-
raise GetoptError('option --%s requires argument' % opt, opt)
157+
raise GetoptError(_('option --%s requires argument') % opt, opt)
157158
optarg, args = args[0], args[1:]
158159
elif optarg is not None:
159-
raise GetoptError('option --%s must not have an argument' % opt, opt)
160+
raise GetoptError(_('option --%s must not have an argument') % opt, opt)
160161
opts.append(('--' + opt, optarg or ''))
161162
return opts, args
162163

@@ -166,7 +167,7 @@ def do_longs(opts, opt, longopts, args):
166167
def long_has_args(opt, longopts):
167168
possibilities = [o for o in longopts if o.startswith(opt)]
168169
if not possibilities:
169-
raise GetoptError('option --%s not recognized' % opt, opt)
170+
raise GetoptError(_('option --%s not recognized') % opt, opt)
170171
# Is there an exact match?
171172
if opt in possibilities:
172173
return False, opt
@@ -176,7 +177,7 @@ def long_has_args(opt, longopts):
176177
if len(possibilities) > 1:
177178
# XXX since possibilities contains all valid continuations, might be
178179
# nice to work them into the error msg
179-
raise GetoptError('option --%s not a unique prefix' % opt, opt)
180+
raise GetoptError(_('option --%s not a unique prefix') % opt, opt)
180181
assert len(possibilities) == 1
181182
unique_match = possibilities[0]
182183
has_arg = unique_match.endswith('=')
@@ -190,7 +191,7 @@ def do_shorts(opts, optstring, shortopts, args):
190191
if short_has_arg(opt, shortopts):
191192
if optstring == '':
192193
if not args:
193-
raise GetoptError('option -%s requires argument' % opt,
194+
raise GetoptError(_('option -%s requires argument') % opt,
194195
opt)
195196
optstring, args = args[0], args[1:]
196197
optarg, optstring = optstring, ''
@@ -203,7 +204,7 @@ def short_has_arg(opt, shortopts):
203204
for i in range(len(shortopts)):
204205
if opt == shortopts[i] != ':':
205206
return shortopts.startswith(':', i+1)
206-
raise GetoptError('option -%s not recognized' % opt, opt)
207+
raise GetoptError(_('option -%s not recognized') % opt, opt)
207208

208209
if __name__ == '__main__':
209210
import sys

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ Core and Builtins
7575
Library
7676
-------
7777

78+
- Issue #11371: Mark getopt error messages as localizable. Patch by Filip
79+
Gruszczyński.
80+
7881
- Issue #4391: Use proper gettext plural forms in optparse.
7982

8083
- Issue #11563: Connection:close header is sent by requests using URLOpener

0 commit comments

Comments
 (0)