19
19
# Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions
20
20
# to class-based exceptions.
21
21
#
22
- # Peter Ã
strand <astrand@lysator.liu.se> added gnu_getopt().
22
+ # Peter Åstrand <astrand@lysator.liu.se> added gnu_getopt().
23
23
#
24
24
# TODO for gnu_getopt():
25
25
#
34
34
__all__ = ["GetoptError" ,"error" ,"getopt" ,"gnu_getopt" ]
35
35
36
36
import os
37
+ from gettext import gettext as _
37
38
38
39
class GetoptError (Exception ):
39
40
opt = ''
@@ -153,10 +154,10 @@ def do_longs(opts, opt, longopts, args):
153
154
if has_arg :
154
155
if optarg is None :
155
156
if not args :
156
- raise GetoptError ('option --%s requires argument' % opt , opt )
157
+ raise GetoptError (_ ( 'option --%s requires argument' ) % opt , opt )
157
158
optarg , args = args [0 ], args [1 :]
158
159
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 )
160
161
opts .append (('--' + opt , optarg or '' ))
161
162
return opts , args
162
163
@@ -166,7 +167,7 @@ def do_longs(opts, opt, longopts, args):
166
167
def long_has_args (opt , longopts ):
167
168
possibilities = [o for o in longopts if o .startswith (opt )]
168
169
if not possibilities :
169
- raise GetoptError ('option --%s not recognized' % opt , opt )
170
+ raise GetoptError (_ ( 'option --%s not recognized' ) % opt , opt )
170
171
# Is there an exact match?
171
172
if opt in possibilities :
172
173
return False , opt
@@ -176,7 +177,7 @@ def long_has_args(opt, longopts):
176
177
if len (possibilities ) > 1 :
177
178
# XXX since possibilities contains all valid continuations, might be
178
179
# 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 )
180
181
assert len (possibilities ) == 1
181
182
unique_match = possibilities [0 ]
182
183
has_arg = unique_match .endswith ('=' )
@@ -190,7 +191,7 @@ def do_shorts(opts, optstring, shortopts, args):
190
191
if short_has_arg (opt , shortopts ):
191
192
if optstring == '' :
192
193
if not args :
193
- raise GetoptError ('option -%s requires argument' % opt ,
194
+ raise GetoptError (_ ( 'option -%s requires argument' ) % opt ,
194
195
opt )
195
196
optstring , args = args [0 ], args [1 :]
196
197
optarg , optstring = optstring , ''
@@ -203,7 +204,7 @@ def short_has_arg(opt, shortopts):
203
204
for i in range (len (shortopts )):
204
205
if opt == shortopts [i ] != ':' :
205
206
return shortopts .startswith (':' , i + 1 )
206
- raise GetoptError ('option -%s not recognized' % opt , opt )
207
+ raise GetoptError (_ ( 'option -%s not recognized' ) % opt , opt )
207
208
208
209
if __name__ == '__main__' :
209
210
import sys
0 commit comments