00001 #!/usr/local/bin/python 00002 # 00003 # Copyright 2004 00004 # by 00005 # The Board of Trustees of the 00006 # Leland Stanford Junior University. 00007 # All rights reserved. 00008 # 00009 00010 00011 __facility__ = "Online" 00012 __abstract__ = "Options class" 00013 __author__ = "Amedeo Perazzo <perazzo@slac.stanford.edu> SLAC - GLAST I&T/Online" 00014 __date__ = ("$Date: 2005/03/06 19:46:21 $").split(' ')[1] 00015 __version__ = "$Revision: 2.5 $" 00016 __release__ = "$Name: R04-12-00 $" 00017 __credits__ = "SLAC" 00018 00019 import LATTE.copyright_SLAC 00020 00021 import sys 00022 import getopt 00023 00024 class Options(object): 00025 def __init__(self, mandatory, optional=[], switches=[]): 00026 self.opts = {} 00027 00028 self.__mand = mandatory 00029 self.__optn = optional 00030 self.__swtc = switches 00031 00032 def __getattr__(self, attribute): 00033 if attribute in self.opts: 00034 return self.opts[attribute] 00035 return None 00036 00037 def usage(self, notes=''): 00038 msg = 'usage: %s' %(sys.argv[0]) 00039 for option in self.__mand: 00040 msg += ' --%s <%sname>' %(option, option) 00041 for option in self.__optn: 00042 msg += ' [--%s <%sname>]' %(option, option) 00043 for option in self.__swtc: 00044 msg += ' [--%s]' %(option) 00045 if notes != '': 00046 msg += '\nnotes: ' 00047 msg += notes 00048 print msg 00049 00050 00051 def parse(self): 00052 opts = [] 00053 for option in self.__mand: 00054 opts.append(option+'=') 00055 for option in self.__optn: 00056 opts.append(option+'=') 00057 for option in self.__swtc: 00058 opts.append(option) 00059 results = getopt.getopt(sys.argv[1:], '', opts) 00060 if len(results[1]) > 0: 00061 raise RuntimeError, 'unknown argument(s) \'%s\'' %(results[1]) 00062 for opt in results[0]: 00063 self.opts[opt[0][2:]] = opt[1] 00064 for option in self.__mand: 00065 if option not in self.opts: 00066 raise RuntimeError, 'mandatory option \'--%s\' not found' %(option)