1919# Gerrit Holl <[email protected] > moved the string-based exceptions 2020# to class-based exceptions.
2121#
22- # Peter Ã
strand <[email protected] > added gnu_getopt(). 22+ # Peter Åstrand <[email protected] > added gnu_getopt(). 2323#
2424# TODO for gnu_getopt():
2525#
3434__all__ = ["GetoptError" ,"error" ,"getopt" ,"gnu_getopt" ]
3535
3636import os
37+ from gettext import gettext as _
3738
3839class 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):
166167def 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
208209if __name__ == '__main__' :
209210 import sys
0 commit comments