1
2 """Parse options for the PyFoam-Scripts"""
3
4 from optparse import OptionParser,TitledHelpFormatter
5 from PyFoam import versionString
6
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,args=None,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 @param args: Command line arguments. If unset sys.argv[1:] is used.
19 Can be a string: it will be splitted then unsing the spaces (very primitive), or a list of strings (prefered)
20 """
21 if usage==None:
22 usage="%prog [options] <foamApplication> <caseDir> <caseName> [foamOptions]"
23
24 if version==None:
25 version="%prog "+versionString()
26
27 if args==None:
28 self.argLine=None
29 elif type(args)==str:
30 self.argLine=args.split()
31 else:
32 self.argLine=map(str,args)
33
34 OptionParser.__init__(self,usage=usage,version=version,description=description,formatter=TitledHelpFormatter())
35
36 if interspersed:
37 self.enable_interspersed_args()
38 else:
39 self.disable_interspersed_args()
40
41 self.options=None
42 self.args=None
43
44 - def parse(self,nr=3,exactNr=True):
45 """
46 parse the options
47 @param nr: minimum number of arguments that are to be passed to the application
48 """
49 (self.options,self.args)=self.parse_args(args=self.argLine)
50
51 if len(self.args)<nr:
52 self.error("Too few arguments (%d needed, %d given)" %(nr,len(self.args)))
53
54 if exactNr and len(self.args)>nr:
55 self.error("Too many arguments (%d needed, %d given)" %(nr,len(self.args)))
56
57 tmp=self.args
58 self.args=[]
59 for a in tmp:
60 if a.find(" ")>=0 or a.find("(")>=0:
61 a="\""+a+"\""
62 self.args.append(a)
63
65 """Return the arguments left after parsing"""
66 if self.args!=None:
67 return self.args
68 else:
69 return []
70
72 """Return the options"""
73 if self.options==None:
74 self.error("options have not been parsed yet")
75
76 return self.options
77