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

Source Code for Module PyFoam.Basics.FoamOptionParser

  1  #  ICE Revision: $Id: FoamOptionParser.py 9441 2008-09-22 20:51:21Z bgschaid $  
  2  """Parse options for the PyFoam-Scripts""" 
  3   
  4  from optparse import OptionParser,TitledHelpFormatter 
  5  from PyFoam import versionString 
  6   
  7  from PyFoam.FoamInformation import changeFoamVersion 
  8  from PyFoam.FoamInformation import oldAppConvention as oldApp 
  9   
 10  from os import path 
 11   
12 -class FoamOptionParser(OptionParser):
13 """Wrapper to the usual OptionParser to honor the conventions of OpenFOAM-utilities 14 15 Options that are not used by the script are passed to the OpenFOAM-application""" 16
17 - def __init__(self,args=None,usage=None,version=None,description=None,interspersed=False):
18 """ 19 @param usage: usage string. If missing a default is used 20 @param version: if missing the PyFoam-version is used 21 @param description: description of the utility 22 @param interspersed: needs to be false if options should be passed to an OpenFOAM-utility 23 @param args: Command line arguments. If unset sys.argv[1:] is used. 24 Can be a string: it will be splitted then unsing the spaces (very primitive), or a list of strings (prefered) 25 """ 26 if usage==None: 27 if oldApp(): 28 usage="%prog [options] <foamApplication> <caseDir> <caseName> [foamOptions]" 29 else: 30 usage="%prog [options] <foamApplication> [foamOptions]" 31 32 if version==None: 33 version="%prog "+versionString() 34 35 if args==None: 36 self.argLine=None 37 elif type(args)==str: 38 self.argLine=args.split() 39 else: 40 self.argLine=map(str,args) 41 42 OptionParser.__init__(self,usage=usage,version=version,description=description,formatter=TitledHelpFormatter()) 43 44 if interspersed: 45 self.enable_interspersed_args() 46 else: 47 self.disable_interspersed_args() 48 49 self.options=None 50 self.args=None
51
52 - def parse(self,nr=None,exactNr=True):
53 """ 54 parse the options 55 @param nr: minimum number of arguments that are to be passed to the application 56 3 is default for pre-1.5 versions of OpenFOAM 57 """ 58 (self.options,self.args)=self.parse_args(args=self.argLine) 59 60 if "foamVersion" in dir(self.options): 61 if self.options.foamVersion!=None: 62 changeFoamVersion(self.options.foamVersion) 63 64 if nr==None: 65 if oldApp(): 66 nr=3 67 else: 68 nr=1 69 70 if len(self.args)<nr: 71 self.error("Too few arguments (%d needed, %d given)" %(nr,len(self.args))) 72 73 maxNr=nr 74 if not oldApp(): 75 if "-case" in self.args: 76 maxNr+=2 77 78 if exactNr and len(self.args)>maxNr: 79 self.error("Too many arguments (%d needed, %d given)" %(nr,len(self.args))) 80 81 tmp=self.args 82 self.args=[] 83 for a in tmp: 84 if a.find(" ")>=0 or a.find("(")>=0: 85 a="\""+a+"\"" 86 self.args.append(a)
87
88 - def getArgs(self):
89 """Return the arguments left after parsing""" 90 if self.args!=None: 91 return self.args 92 else: 93 return []
94
95 - def getOptions(self):
96 """Return the options""" 97 if self.options==None: 98 self.error("options have not been parsed yet") 99 100 return self.options
101
102 - def casePath(self):
103 """Returns the path to the case (if applicable)""" 104 if oldApp(): 105 return path.join(self.getArgs()[1],self.getArgs()[2]) 106 else: 107 if "-case" in self.getArgs(): 108 return path.normpath(self.getArgs()[self.getArgs().index("-case")+1]) 109 else: 110 return path.abspath(path.curdir)
111