Package PyFoam :: Package Basics :: Module FoamOptionParser
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.FoamOptionParser

 1  #  ICE Revision: $Id: FoamOptionParser.py 7581 2007-06-27 15:29:14Z bgschaid $  
 2  """Parse options for the PyFoam-Scripts""" 
 3   
 4  from optparse import OptionParser 
 5  from PyFoam import versionString 
 6   
7 -class FoamOptionParser(OptionParser):
8 """Wrapper to the usual OptionParser to honor the conventions of OpenFOAM-utilities 9 10 Options that are not used by the script are passed to the OpenFOAM-application""" 11
12 - def __init__(self,usage=None,version=None,description=None,interspersed=False):
13 """ 14 @param usage: usage string. If missing a default is used 15 @param version: if missing the PyFoam-version is used 16 @param description: description of the utility 17 @param interspersed: needs to be false if options should be passed to an OpenFOAM-utility 18 """ 19 if usage==None: 20 usage="%prog [options] <foamApplication> <caseDir> <caseName> [foamOptions]" 21 22 if version==None: 23 version="%prog "+versionString() 24 25 OptionParser.__init__(self,usage=usage,version=version,description=description) 26 27 if interspersed: 28 self.enable_interspersed_args() 29 else: 30 self.disable_interspersed_args() 31 32 self.options=None 33 self.args=None
34
35 - def parse(self,nr=3):
36 """ 37 parse the options 38 @param nr: minimum number of arguments that are to be passed to the application 39 """ 40 (self.options,self.args)=self.parse_args() 41 42 if len(self.args)<nr: 43 self.error("Too few arguments (%d needed, %d given)" %(nr,len(self.args))) 44 45 tmp=self.args 46 self.args=[] 47 for a in tmp: 48 if a.find(" ")>=0 or a.find("(")>=0: 49 a="\""+a+"\"" 50 self.args.append(a)
51
52 - def getArgs(self):
53 """Return the arguments left after parsing""" 54 if self.args!=None: 55 return self.args 56 else: 57 return []
58
59 - def getOptions(self):
60 """Return the options""" 61 if self.options==None: 62 self.error("options have not been parsed yet") 63 64 return self.options
65