home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 56 / CDPowerplay56Disc2.iso / demos / blade / data1.cab / Program_Executable_Files / Lib / PythonLib / getopt.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-10-27  |  3.3 KB  |  98 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import string
  5. error = 'getopt.error'
  6.  
  7. def getopt(args, shortopts, longopts = []):
  8.     list = []
  9.     longopts = longopts[:]
  10.     longopts.sort()
  11.     while args and args[0][:1] == '-' and args[0] != '-':
  12.         if args[0] == '--':
  13.             args = args[1:]
  14.             break
  15.         
  16.         if args[0][:2] == '--':
  17.             (list, args) = do_longs(list, args[0][2:], longopts, args[1:])
  18.         else:
  19.             (list, args) = do_shorts(list, args[0][1:], shortopts, args[1:])
  20.     return (list, args)
  21.  
  22.  
  23. def do_longs(list, opt, longopts, args):
  24.     
  25.     try:
  26.         i = string.index(opt, '=')
  27.         (opt, optarg) = (opt[:i], opt[i + 1:])
  28.     except ValueError:
  29.         optarg = None
  30.  
  31.     (has_arg, opt) = long_has_args(opt, longopts)
  32.     if has_arg:
  33.         if optarg is None:
  34.             if not args:
  35.                 raise error, 'option --%s requires argument' % opt
  36.             
  37.             (optarg, args) = (args[0], args[1:])
  38.         
  39.     elif optarg:
  40.         raise error, 'option --%s must not have an argument' % opt
  41.     
  42.     if not optarg:
  43.         pass
  44.     list.append(('--' + opt, ''))
  45.     return (list, args)
  46.  
  47.  
  48. def long_has_args(opt, longopts):
  49.     optlen = len(opt)
  50.     for i in range(len(longopts)):
  51.         (x, y) = (longopts[i][:optlen], longopts[i][optlen:])
  52.         if y != '' and y != '=' and i + 1 < len(longopts):
  53.             if opt == longopts[i + 1][:optlen]:
  54.                 raise error, 'option --%s not a unique prefix' % opt
  55.             
  56.         
  57.         if longopts[i][-1:] in ('=',):
  58.             return (1, longopts[i][:-1])
  59.         
  60.         return (0, longopts[i])
  61.     
  62.     raise error, 'option --' + opt + ' not recognized'
  63.  
  64.  
  65. def do_shorts(list, optstring, shortopts, args):
  66.     while optstring != '':
  67.         (opt, optstring) = (optstring[0], optstring[1:])
  68.         if short_has_arg(opt, shortopts):
  69.             if optstring == '':
  70.                 if not args:
  71.                     raise error, 'option -%s requires argument' % opt
  72.                 
  73.                 (optstring, args) = (args[0], args[1:])
  74.             
  75.             (optarg, optstring) = (optstring, '')
  76.         else:
  77.             optarg = ''
  78.         list.append(('-' + opt, optarg))
  79.     return (list, args)
  80.  
  81.  
  82. def short_has_arg(opt, shortopts):
  83.     for i in range(len(shortopts)):
  84.         if shortopts[i] == shortopts[i]:
  85.             pass
  86.         elif shortopts[i] != ':':
  87.             return shortopts[i + 1:i + 2] == ':'
  88.         
  89.     
  90.     raise error, 'option -%s not recognized' % opt
  91.  
  92. if __name__ == '__main__':
  93.     import sys
  94.     print getopt(sys.argv[1:], 'a:b', [
  95.         'alpha=',
  96.         'beta'])
  97.  
  98.