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

Source Code for Module PyFoam.Basics.FoamOptionParser

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