FreeFOAM The Cross-Platform CFD Toolkit
argList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::argList
26 
27 Description
28  Extract command arguments and options from the supplied
29  @a argc and @a argv parameters.
30 
31  Sequences with "(" ... ")" are transformed into a stringList.
32  For example,
33  @verbatim
34  program -listFiles \( *.txt \)
35  @endverbatim
36  would create a stringList:
37  @verbatim
38  ( "file1.txt" "file2.txt" ... "fileN.txt" )
39  @endverbatim
40  The backslash-escaping has been used to avoid shell expansions.
41 
42  @par Default command-line options
43  @param -case <dir> \n
44  select an case directory instead of the current working directory
45  @param -parallel \n
46  specify case as a parallel job
47  @param -doc \n
48  display the documentation in browser
49  @param -srcDoc \n
50  display the source documentation in browser
51  @param -help \n
52  print the usage
53 
54  The environment variable @b FOAM_CASE is set to the path of the
55  global case (same for serial and parallel jobs).
56  The environment variable @b FOAM_CASENAME is set to the name of the
57  global case.
58 
59 Note
60  - Adjustment of the valid (mandatory) arguments
61  by directly manipulating the static member argList::validArgs.
62  - Adjustment of the valid options
63  by directly manipulating the static member argList::validOptions.
64 
65 SourceFiles
66  argList.C
67 
68 \*---------------------------------------------------------------------------*/
69 
70 #ifndef argList_H
71 #define argList_H
72 
73 #include <OpenFOAM/stringList.H>
74 #include <OpenFOAM/SubList.H>
75 #include <OpenFOAM/SLList.H>
76 #include <OpenFOAM/HashTable.H>
77 #include <OpenFOAM/word.H>
78 #include <OpenFOAM/fileName.H>
79 #include "parRun.H"
80 #include <OpenFOAM/IStringStream.H>
81 
82 #include <OSspecific/sigFpe.H>
83 #include <OSspecific/sigInt.H>
84 #include <OSspecific/sigQuit.H>
85 #include <OSspecific/sigSegv.H>
86 
87 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
88 
89 namespace Foam
90 {
91 
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93 
94 class argList
95 {
96  // Private data
97  static bool bannerEnabled;
98 
99  stringList args_;
100  HashTable<string> options_;
101 
102  word executable_;
103  fileName rootPath_;
104  fileName globalCase_;
105  fileName case_;
106 
107  ParRunControl parRunControl_;
108 
109  // Signal handlers
110  sigFpe sigFpe_;
111  sigInt sigInt_;
112  sigQuit sigQuit_;
113  sigSegv sigSegv_;
114 
115 
116  // Private member functions
117 
118  void getRootCase();
119 
120  //- Transcribe argv into internal args_
121  // return true if any "(" ... ")" sequences were captured
122  bool regroupArgv(int& argc, char**& argv);
123 
124 
125 public:
126 
127  // Static data members
128 
129  //- A list of valid (mandatory) arguments
131 
132  //- A list of valid options
134 
135  //- A list of valid parallel options
137 
139  class initValidTables
140  {
141  public:
142 
143  initValidTables();
144  };
146 
147 
148  // Constructors
149 
150  //- Construct from argc and argv
151  // checking the arguments and options as requested
152  argList
153  (
154  int& argc,
155  char**& argv,
156  bool checkArgs=true,
157  bool checkOpts=true
158  );
159 
160 
161  // Destructor
162 
163  virtual ~argList();
164 
165 
166  // Member functions
167 
168  // Access
169 
170  //- Name of executable
171  const word& executable() const
172  {
173  return executable_;
174  }
175 
176  //- Return root path
177  const fileName& rootPath() const
178  {
179  return rootPath_;
180  }
181 
182  //- Return case name
183  const fileName& globalCaseName() const
184  {
185  return globalCase_;
186  }
187 
188  //- Return case name (parallel run) or global case (serial run)
189  const fileName& caseName() const
190  {
191  return case_;
192  }
193 
194  //- Return the path
195  fileName path() const
196  {
197  return rootPath()/caseName();
198  }
199 
200  //- Return arguments
201  const stringList& args() const
202  {
203  return args_;
204  }
205 
206  //- Return additionl arguments,
207  // i.e. those additional to the executable itself
209 
210  //- Return options
212  {
213  return options_;
214  }
215 
216  //- Return the argument string associated with the named option
217  const string& option(const word& opt) const
218  {
219  return options_.operator[](opt);
220  }
221 
222  //- Return true if the named option is found
223  bool optionFound(const word& opt) const
224  {
225  return options_.found(opt);
226  }
227 
228  //- Return an IStringStream to the named option
229  IStringStream optionLookup(const word& opt) const
230  {
231  return IStringStream(option(opt));
232  }
233 
234  //- Read a value from the named option
235  template<class T>
236  T optionRead(const word& opt) const
237  {
238  T val;
239  optionLookup(opt)() >> val;
240  return val;
241  }
242 
243  //- Read a value from the named option if present.
244  // Return true if the named option was found.
245  template<class T>
246  bool optionReadIfPresent(const word& opt, T& val) const
247  {
248  if (optionFound(opt))
249  {
250  optionLookup(opt)() >> val;
251  return true;
252  }
253  else
254  {
255  return false;
256  }
257  }
258 
259  //- Read a List of values from the named option
260  template<class T>
261  List<T> optionReadList(const word& opt) const
262  {
263  return readList<T>(optionLookup(opt)());
264  }
265 
266 
267  // Edit
268 
269  //- Disable emitting the banner information
270  static void noBanner();
271 
272  //- Remove the parallel options
273  static void noParallel();
274 
275 
276  // Print
277 
278  //- Print usage
279  void printUsage() const;
280 
281  //- Display documentation in browser
282  // Optionally display the application source code
283  void displayDoc(bool source=false) const;
284 
285 
286  // Check
287 
288  //- Check argument list
289  bool check(bool checkArgs=true, bool checkOpts=true) const;
290 
291  //- Check root path and case path
292  bool checkRootCase() const;
293 };
294 
295 
296 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
297 
298 } // End namespace Foam
299 
300 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
301 
302 #endif
303 
304 // ************************ vim: set sw=4 sts=4 et: ************************ //