1
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 PyFoam.Error import error,warning
11
12 from os import path
13
15 """Wrapper to the usual OptionParser to honor the conventions of OpenFOAM-utilities
16
17 Options that are not used by the script are passed to the OpenFOAM-application"""
18
19 - def __init__(self,args=None,usage=None,version=None,description=None,interspersed=False):
20 """
21 @param usage: usage string. If missing a default is used
22 @param version: if missing the PyFoam-version is used
23 @param description: description of the utility
24 @param interspersed: needs to be false if options should be passed to an OpenFOAM-utility
25 @param args: Command line arguments. If unset sys.argv[1:] is used.
26 Can be a string: it will be splitted then unsing the spaces (very primitive), or a list of strings (prefered)
27 """
28 if usage==None:
29 if oldApp():
30 usage="%prog [options] <foamApplication> <caseDir> <caseName> [foamOptions]"
31 else:
32 usage="%prog [options] <foamApplication> [foamOptions]"
33
34 if version==None:
35 version="%prog "+versionString()
36
37 if args==None:
38 self.argLine=None
39 elif type(args)==str:
40 self.argLine=args.split()
41 else:
42 self.argLine=map(str,args)
43
44 OptionParser.__init__(self,usage=usage,version=version,description=description,formatter=TitledHelpFormatter())
45
46 if interspersed:
47 self.enable_interspersed_args()
48 else:
49 self.disable_interspersed_args()
50
51 self.options=None
52 self.args=None
53
54 - def parse(self,nr=None,exactNr=True):
55 """
56 parse the options
57 @param nr: minimum number of arguments that are to be passed to the application
58 3 is default for pre-1.5 versions of OpenFOAM
59 """
60 (self.options,self.args)=self.parse_args(args=self.argLine)
61
62 if "foamVersion" in dir(self.options):
63 if self.options.foamVersion!=None:
64 if self.options.force32 and self.options.force64:
65 error("A version can't be 32 and 64 bit at the same time")
66 changeFoamVersion(self.options.foamVersion,
67 force64=self.options.force64,
68 force32=self.options.force32,
69 compileOption=self.options.compileOption)
70 elif self.options.force32 or self.options.force64:
71 warning("Foring version to be 32 or 64 bit, but no version chosen. Doing nothing")
72
73 if nr==None:
74 if oldApp():
75 nr=3
76 else:
77 nr=1
78
79 if len(self.args)<nr:
80 self.error("Too few arguments (%d needed, %d given)" %(nr,len(self.args)))
81
82 maxNr=nr
83 if not oldApp():
84 if "-case" in self.args:
85 maxNr+=2
86
87 if exactNr and len(self.args)>maxNr:
88 self.error("Too many arguments (%d needed, %d given)" %(nr,len(self.args)))
89
90 tmp=self.args
91 self.args=[]
92 for a in tmp:
93 if a.find(" ")>=0 or a.find("(")>=0:
94 a="\""+a+"\""
95 self.args.append(a)
96
98 """Return the arguments left after parsing"""
99 if self.args!=None:
100 return self.args
101 else:
102 return []
103
105 """Return the OpenFOAM-Application to be run"""
106 if self.args!=None:
107 return self.args[0]
108 else:
109 return None
110
112 """Return the options"""
113 if self.options==None:
114 self.error("options have not been parsed yet")
115
116 return self.options
117
119 """Returns the path to the case (if applicable)"""
120 if oldApp():
121 return path.join(self.getArgs()[1],self.getArgs()[2])
122 else:
123 if "-case" in self.getArgs():
124 return path.normpath(self.getArgs()[self.getArgs().index("-case")+1])
125 else:
126 return path.abspath(path.curdir)
127