From: Finn B. <bc...@us...> - 2000-11-17 20:46:21
|
Update of /cvsroot/jython/jython/Lib In directory slayer.i.sourceforge.net:/tmp/cvs-serv11911 Modified Files: getopt.py Log Message: getopt.py from CPython-2.0. Index: getopt.py =================================================================== RCS file: /cvsroot/jython/jython/Lib/getopt.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** getopt.py 1999/06/08 20:10:03 1.1 --- getopt.py 2000/11/17 20:46:17 1.2 *************** *** 1,3 **** ! """Module getopt -- Parser for command line options. This module helps scripts to parse the command line arguments in --- 1,3 ---- ! """Parser for command line options. This module helps scripts to parse the command line arguments in *************** *** 9,21 **** getopt() -- Parse command line options ! error -- Exception (string) raised when bad options are found """ # Long option support added by Lars Wirzenius <li...@ik...>. ! import string ! error = 'getopt.error' def getopt(args, shortopts, longopts = []): """getopt(args, options[, long_options]) -> opts, args --- 9,37 ---- getopt() -- Parse command line options ! GetoptError -- exception (class) raised with 'opt' attribute, which is the ! option involved with the exception. """ # Long option support added by Lars Wirzenius <li...@ik...>. ! # Gerrit Holl <ge...@nl...> moved the string-based exceptions ! # to class-based exceptions. ! class GetoptError(Exception): ! opt = '' ! msg = '' ! def __init__(self, *args): ! self.args = args ! if len(args) == 1: ! self.msg = args[0] ! elif len(args) == 2: ! self.msg = args[0] ! self.opt = args[1] + def __str__(self): + return self.msg + + error = GetoptError # backward compatibility + def getopt(args, shortopts, longopts = []): """getopt(args, options[, long_options]) -> opts, args *************** *** 64,68 **** def do_longs(opts, opt, longopts, args): try: ! i = string.index(opt, '=') opt, optarg = opt[:i], opt[i+1:] except ValueError: --- 80,84 ---- def do_longs(opts, opt, longopts, args): try: ! i = opt.index('=') opt, optarg = opt[:i], opt[i+1:] except ValueError: *************** *** 73,80 **** if optarg is None: if not args: ! raise error, 'option --%s requires argument' % opt optarg, args = args[0], args[1:] elif optarg: ! raise error, 'option --%s must not have an argument' % opt opts.append(('--' + opt, optarg or '')) return opts, args --- 89,96 ---- if optarg is None: if not args: ! raise GetoptError('option --%s requires argument' % opt, opt) optarg, args = args[0], args[1:] elif optarg: ! raise GetoptError('option --%s must not have an argument' % opt, opt) opts.append(('--' + opt, optarg or '')) return opts, args *************** *** 91,99 **** if y != '' and y != '=' and i+1 < len(longopts): if opt == longopts[i+1][:optlen]: ! raise error, 'option --%s not a unique prefix' % opt if longopts[i][-1:] in ('=', ): return 1, longopts[i][:-1] return 0, longopts[i] ! raise error, 'option --' + opt + ' not recognized' def do_shorts(opts, optstring, shortopts, args): --- 107,115 ---- if y != '' and y != '=' and i+1 < len(longopts): if opt == longopts[i+1][:optlen]: ! raise GetoptError('option --%s not a unique prefix' % opt, opt) if longopts[i][-1:] in ('=', ): return 1, longopts[i][:-1] return 0, longopts[i] ! raise GetoptError('option --%s not recognized' % opt, opt) def do_shorts(opts, optstring, shortopts, args): *************** *** 103,107 **** if optstring == '': if not args: ! raise error, 'option -%s requires argument' % opt optstring, args = args[0], args[1:] optarg, optstring = optstring, '' --- 119,123 ---- if optstring == '': if not args: ! raise GetoptError('option -%s requires argument' % opt, opt) optstring, args = args[0], args[1:] optarg, optstring = optstring, '' *************** *** 115,119 **** if opt == shortopts[i] != ':': return shortopts[i+1:i+2] == ':' ! raise error, 'option -%s not recognized' % opt if __name__ == '__main__': --- 131,135 ---- if opt == shortopts[i] != ':': return shortopts[i+1:i+2] == ':' ! raise GetoptError('option -%s not recognized' % opt, opt) if __name__ == '__main__': |