home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / getopt.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  3.8 KB  |  123 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. __all__ = [
  5.     'GetoptError',
  6.     'error',
  7.     'getopt']
  8.  
  9. class GetoptError(Exception):
  10.     opt = ''
  11.     msg = ''
  12.     
  13.     def __init__(self, msg, opt):
  14.         self.msg = msg
  15.         self.opt = opt
  16.         Exception.__init__(self, msg, opt)
  17.  
  18.     
  19.     def __str__(self):
  20.         return self.msg
  21.  
  22.  
  23. error = GetoptError
  24.  
  25. def getopt(args, shortopts, longopts = []):
  26.     opts = []
  27.     if type(longopts) == type(''):
  28.         longopts = [
  29.             longopts]
  30.     else:
  31.         longopts = list(longopts)
  32.     while args and args[0].startswith('-') and args[0] != '-':
  33.         if args[0] == '--':
  34.             args = args[1:]
  35.             break
  36.         
  37.         if args[0].startswith('--'):
  38.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  39.         else:
  40.             (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  41.     return (opts, args)
  42.  
  43.  
  44. def do_longs(opts, opt, longopts, args):
  45.     
  46.     try:
  47.         i = opt.index('=')
  48.     except ValueError:
  49.         optarg = None
  50.  
  51.     (opt, optarg) = (opt[:i], opt[i + 1:])
  52.     (has_arg, opt) = long_has_args(opt, longopts)
  53.     if has_arg:
  54.         if optarg is None:
  55.             if not args:
  56.                 raise GetoptError('option --%s requires argument' % opt, opt)
  57.             
  58.             (optarg, args) = (args[0], args[1:])
  59.         
  60.     elif optarg:
  61.         raise GetoptError('option --%s must not have an argument' % opt, opt)
  62.     
  63.     if not optarg:
  64.         pass
  65.     opts.append(('--' + opt, ''))
  66.     return (opts, args)
  67.  
  68.  
  69. def long_has_args(opt, longopts):
  70.     possibilities = None if o.startswith(opt) else []
  71.     if not possibilities:
  72.         raise GetoptError('option --%s not recognized' % opt, opt)
  73.     
  74.     if opt in possibilities:
  75.         return (0, opt)
  76.     elif opt + '=' in possibilities:
  77.         return (1, opt)
  78.     
  79.     if len(possibilities) > 1:
  80.         raise GetoptError('option --%s not a unique prefix' % opt, opt)
  81.     
  82.     unique_match = possibilities[0]
  83.     has_arg = unique_match.endswith('=')
  84.     if has_arg:
  85.         unique_match = unique_match[:-1]
  86.     
  87.     return (has_arg, unique_match)
  88.  
  89.  
  90. def do_shorts(opts, optstring, shortopts, args):
  91.     while optstring != '':
  92.         (opt, optstring) = (optstring[0], optstring[1:])
  93.         if short_has_arg(opt, shortopts):
  94.             if optstring == '':
  95.                 if not args:
  96.                     raise GetoptError('option -%s requires argument' % opt, opt)
  97.                 
  98.                 (optstring, args) = (args[0], args[1:])
  99.             
  100.             (optarg, optstring) = (optstring, '')
  101.         else:
  102.             optarg = ''
  103.         opts.append(('-' + opt, optarg))
  104.     return (opts, args)
  105.  
  106.  
  107. def short_has_arg(opt, shortopts):
  108.     for i in range(len(shortopts)):
  109.         if shortopts[i] == shortopts[i]:
  110.             pass
  111.         elif shortopts[i] != ':':
  112.             return shortopts.startswith(':', i + 1)
  113.         
  114.     
  115.     raise GetoptError('option -%s not recognized' % opt, opt)
  116.  
  117. if __name__ == '__main__':
  118.     import sys
  119.     print getopt(sys.argv[1:], 'a:b', [
  120.         'alpha=',
  121.         'beta'])
  122.  
  123.