[Python-checkins] CVS: python/dist/src/Lib getopt.py,1.7,1.8

Guido van Rossum guido@cnri.reston.va.us
Tue, 21 Dec 1999 17:38:43 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Lib
In directory eric:/projects/python/develop/guido/src/Lib

Modified Files:
	getopt.py 
Log Message:
Contribution from Gerrit Holl:

This patch changes the string-based exceptions to class-based
exceptions, so that you can fetch the unknown option as an
attribute.  As far as I know, it is backward compatible.

[The new exception class is called GetoptError; the name error is an
alias for compatibility.]


Index: getopt.py
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Lib/getopt.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** getopt.py	1998/11/17 04:16:37	1.7
--- getopt.py	1999/12/21 22:38:40	1.8
***************
*** 8,13 ****
  provides a single function and an exception:
  
  getopt() -- Parse command line options
! error    -- Exception (string) raised when bad options are found
  """
  
--- 8,17 ----
  provides a single function and an exception:
  
+ Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions
+ to class-based exceptions.
+ 
  getopt() -- Parse command line options
! GetoptError -- exception (class) raised with 'opt' attribute, which is the
! option involved with the exception.
  """
  
***************
*** 15,20 ****
  
  import string
  
! error = 'getopt.error'
  
  def getopt(args, shortopts, longopts = []):
--- 19,38 ----
  
  import string
+ 
+ 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 = []):
***************
*** 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
--- 91,98 ----
          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):
--- 109,117 ----
          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, ''
--- 121,125 ----
              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__':
--- 133,137 ----
          if opt == shortopts[i] != ':':
              return shortopts[i+1:i+2] == ':'
!     raise GetoptError('option -%s not recognized' % opt, opt)
  
  if __name__ == '__main__':