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) 2011-2017 OpenFOAM Foundation
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 is required to avoid interpretation by the shell.
41 
42  Default command-line options:
43  - \par -case <dir>
44  Select a case directory instead of the current working directory
45  - \par -parallel
46  Specify case as a parallel job
47  - \par -doc
48  Display the documentation in browser
49  - \par -srcDoc
50  Display the source documentation in browser
51  - \par -help
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  - The document browser used is defined by the \b FOAM_DOC_BROWSER
61  environment variable or the <tt>Documentation/docBrowser</tt> entry
62  in the <tt>~OpenFOAM/controlDict</tt> file.
63  The \%f token is used as a placeholder for the file name.
64  - The valid (mandatory) arguments can be adjusted
65  by directly manipulating the argList::validArgs static member.
66  - The valid options can be adjusted
67  via the addOption/removeOption static methods instead of directly
68  manipulating the argList::validOptions static member.
69 
70 SourceFiles
71  argList.C
72  argListI.H
73 
74 \*---------------------------------------------------------------------------*/
75 
76 #ifndef argList_H
77 #define argList_H
78 
79 #include "stringList.H"
80 #include "SubList.H"
81 #include "SLList.H"
82 #include "HashTable.H"
83 #include "word.H"
84 #include "fileName.H"
85 #include "parRun.H"
86 #include "IStringStream.H"
87 #include "OSspecific.H"
88 
89 #include "sigFpe.H"
90 #include "sigInt.H"
91 #include "sigQuit.H"
92 #include "sigSegv.H"
93 
94 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
95 
96 namespace Foam
97 {
98 
99 /*---------------------------------------------------------------------------*\
100  Class argList Declaration
101 \*---------------------------------------------------------------------------*/
103 class argList
104 {
105  // Private data
106  static bool bannerEnabled;
107 
108  //- Switch on/off parallel mode. Has to be first to be constructed
109  // so destructor is done last.
110  ParRunControl parRunControl_;
111 
112  stringList args_;
113  HashTable<string> options_;
114 
115  word executable_;
116  fileName rootPath_;
117  fileName globalCase_;
118  fileName case_;
119  string argListStr_;
120 
121  // Signal handlers
122  sigFpe sigFpe_;
123  sigInt sigInt_;
124  sigQuit sigQuit_;
125  sigSegv sigSegv_;
126 
127 
128  // Private Member Functions
129 
130  //- Helper function for printUsage
131  static void printOptionUsage
132  (
133  const label location,
134  const string& str
135  );
136 
137  //- Get rootPath_ / globalCase_ from one of the following forms
138  // * [-case dir]
139  // * cwd
140  //
141  // Also export FOAM_CASE and FOAM_CASENAME environment variables
142  // so they can be used immediately (eg, in decomposeParDict)
143  void getRootCase();
144 
145  //- Transcribe argv into internal args_
146  // return true if any "(" ... ")" sequences were captured
147  bool regroupArgv(int& argc, char**& argv);
148 
149 
150 public:
151 
152  // Static data members
153 
154  //- A list of valid (mandatory) arguments
155  static SLList<string> validArgs;
156 
157  //- A list of valid options
159 
160  //- A list of valid parallel options
162 
163  //- Short usage information for validOptions
165 
166  //- Additional notes for usage
167  static SLList<string> notes;
168 
169  //- Min offset for displaying usage (default: 20)
171 
172  //- Max screen width for displaying usage (default: 80)
174 
175  //- Standard name for the post-processing option
177 
179  class initValidTables
180  {
181  public:
182 
183  initValidTables();
184  };
186 
187 
188  // Constructors
189 
190  //- Construct from argc and argv
191  // checking the arguments and options as requested
192  argList
193  (
194  int& argc,
195  char**& argv,
196  bool checkArgs = true,
197  bool checkOpts = true,
198  bool initialise = true
199  );
200 
201  //- Construct copy with new options
202  argList
203  (
204  const argList& args,
205  const HashTable<string>& options,
206  bool checkArgs = true,
207  bool checkOpts = true,
208  bool initialise = true
209  );
210 
211 
212  //- Destructor
213  virtual ~argList();
214 
215 
216  // Member functions
217 
218  //- Parse
219  void parse
220  (
221  bool checkArgs,
222  bool checkOpts,
223  bool initialise
224  );
225 
226 
227  // Access
228 
229  //- Name of executable without the path
230  inline const word& executable() const;
231 
232  //- Return root path
233  inline const fileName& rootPath() const;
234 
235  //- Return case name (parallel run) or global case (serial run)
236  inline const fileName& caseName() const;
237 
238  //- Return case name
239  inline const fileName& globalCaseName() const;
240 
241  //- Return parRunControl
242  inline const ParRunControl& parRunControl() const;
243 
244  //- Return the path to the caseName
245  inline fileName path() const;
246 
247  //- Return arguments
248  inline const stringList& args() const;
249 
250  //- Return non-const access to arguments
251  inline stringList& args();
252 
253  //- Return the argument corresponding to index.
254  inline const string& arg(const label index) const;
255 
256  //- Return the number of arguments
257  inline label size() const;
258 
259  //- Read a value from the argument at index.
260  // Index 0 corresponds to the name of the executable.
261  // Index 1 corresponds to the first argument.
262  template<class T>
263  inline T argRead(const label index) const;
264 
265  //- Return options
266  inline const Foam::HashTable<string>& options() const;
267 
268  //- Return non-const access to options
270 
271  //- Return the argument string associated with the named option
272  inline const string& option(const word& opt) const;
273 
274  //- Return true if the named option is found
275  inline bool optionFound(const word& opt) const;
276 
277  //- Return an IStringStream from the named option
278  inline IStringStream optionLookup(const word& opt) const;
279 
280  //- Read a value from the named option
281  template<class T>
282  inline T optionRead(const word& opt) const;
283 
284  //- Read a value from the named option if present.
285  // Return true if the named option was found.
286  template<class T>
287  inline bool optionReadIfPresent(const word& opt, T&) const;
288 
289  //- Read a value from the named option if present.
290  // Return true if the named option was found, otherwise
291  // use the supplied default and return false.
292  template<class T>
293  inline bool optionReadIfPresent
294  (
295  const word& opt,
296  T&,
297  const T& deflt
298  ) const;
299 
300  //- Read a value from the named option if present.
301  // Return supplied default otherwise.
302  template<class T>
303  inline T optionLookupOrDefault
304  (
305  const word& opt,
306  const T& deflt
307  ) const;
308 
309  //- Read a List of values from the named option
310  template<class T>
311  List<T> optionReadList(const word& opt) const
312  {
313  return readList<T>(optionLookup(opt)());
314  }
315 
316 
317  //- Return the argument corresponding to index.
318  // Index 0 corresponds to the name of the executable.
319  // Index 1 corresponds to the first argument.
320  inline const string& operator[](const label index) const;
321 
322  //- Return the argument string associated with the named option
323  // \sa option()
324  inline const string& operator[](const word& opt) const;
325 
326 
327  // Edit
328 
329  //- Add to a bool option to validOptions with usage information
330  static void addBoolOption
331  (
332  const word& opt,
333  const string& usage = ""
334  );
335 
336  //- Add to an option to validOptions with usage information
337  // An option with an empty param is a bool option
338  static void addOption
339  (
340  const word& opt,
341  const string& param = "",
342  const string& usage = ""
343  );
344 
345  //- Add option usage information to optionUsage
346  static void addUsage
347  (
348  const word& opt,
349  const string& usage
350  );
351 
352  //- Add extra notes for the usage information
353  // This string is used "as-is" without additional formatting
354  static void addNote(const string&);
355 
356  //- Remove option from validOptions and from optionUsage
357  static void removeOption(const word& opt);
358 
359  //- Disable emitting the banner information
360  static void noBanner();
361 
362  //- Remove the parallel options
363  static void noParallel();
364 
365  //- Return true if the post-processing option is specified
366  static bool postProcess(int argc, char *argv[]);
367 
368  //- Set option directly (use with caution)
369  // An option with an empty param is a bool option.
370  // Not all valid options can also be set: eg, -case, -roots, ...
371  // Return true if the existing option value needed changing,
372  // or if the option did not previously exist.
373  bool setOption(const word& opt, const string& param = "");
374 
375  //- Unset option directly (use with caution)
376  // Not all valid options can also be unset: eg, -case, -roots ...
377  // Return true if the option existed before being unset.
378  bool unsetOption(const word& opt);
379 
380 
381  // Print
382 
383  //- Print notes (if any)
384  void printNotes() const;
385 
386  //- Print usage
387  void printUsage() const;
388 
389  //- Display documentation in browser
390  // Optionally display the application source code
391  void displayDoc(bool source=false) const;
392 
393 
394  // Check
395 
396  //- Check argument list
397  bool check(bool checkArgs=true, bool checkOpts=true) const;
398 
399  //- Check root path and case path
400  bool checkRootCase() const;
401 };
402 
403 
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 
406 } // End namespace Foam
407 
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 
410 #include "argListI.H"
411 
412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
413 
414 #endif
415 
416 // ************************************************************************* //
bool checkRootCase() const
Check root path and case path.
Definition: argList.C:1287
const word & executable() const
Name of executable without the path.
Definition: argListI.H:30
bool setOption(const word &opt, const string &param="")
Set option directly (use with caution)
Definition: argList.C:968
static void removeOption(const word &opt)
Remove option from validOptions and from optionUsage.
Definition: argList.C:135
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling file names.
Definition: fileName.H:69
Helper class for initializing parallel jobs from the command arguments.
Definition: parRun.H:46
static word postProcessOptionName
Standard name for the post-processing option.
Definition: argList.H:175
bool unsetOption(const word &opt)
Unset option directly (use with caution)
Definition: argList.C:1043
const fileName & globalCaseName() const
Return case name.
Definition: argListI.H:48
static void addUsage(const word &opt, const string &usage)
Add option usage information to optionUsage.
Definition: argList.C:110
const string & operator[](const label index) const
Return the argument corresponding to index.
Definition: argListI.H:255
argList(int &argc, char **&argv, bool checkArgs=true, bool checkOpts=true, bool initialise=true)
Construct from argc and argv.
Definition: argList.C:388
const fileName & rootPath() const
Return root path.
Definition: argListI.H:36
Template class for non-intrusive linked lists.
Definition: LList.H:51
static SLList< string > notes
Additional notes for usage.
Definition: argList.H:166
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
static void noParallel()
Remove the parallel options.
Definition: argList.C:148
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
static bool postProcess(int argc, char *argv[])
Return true if the post-processing option is specified.
Definition: argList.C:256
virtual ~argList()
Destructor.
Definition: argList.C:956
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
const ParRunControl & parRunControl() const
Return parRunControl.
Definition: argListI.H:54
void parse(bool checkArgs, bool checkOpts, bool initialise)
Parse.
Definition: argList.C:506
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Signal handler for QUIT interupt.
Definition: sigQuit.H:54
void displayDoc(bool source=false) const
Display documentation in browser.
Definition: argList.C:1169
const Foam::HashTable< string > & options() const
Return options.
Definition: argListI.H:90
T optionRead(const word &opt) const
Read a value from the named option.
Definition: argListI.H:187
static HashTable< string > validParOptions
A list of valid parallel options.
Definition: argList.H:160
void printNotes() const
Print notes (if any)
Definition: argList.C:1078
Set up trapping for floating point exceptions (signal FPE).
Definition: sigFpe.H:67
A class for handling words, derived from string.
Definition: word.H:59
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:95
label size() const
Return the number of arguments.
Definition: argListI.H:84
static HashTable< string > optionUsage
Short usage information for validOptions.
Definition: argList.H:163
An STL-conforming hash table.
Definition: HashTable.H:62
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:42
Signal handler for INT interupt.
Definition: sigInt.H:54
const string & arg(const label index) const
Return the argument corresponding to index.
Definition: argListI.H:78
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
fileName path() const
Return the path to the caseName.
Definition: argListI.H:60
Input from memory buffer stream.
Definition: IStringStream.H:49
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:177
List< T > optionReadList(const word &opt) const
Read a List of values from the named option.
Definition: argList.H:310
bool check(bool checkArgs=true, bool checkOpts=true) const
Check argument list.
Definition: argList.C:1246
static void noBanner()
Disable emitting the banner information.
Definition: argList.C:142
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:85
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:126
static string::size_type usageMin
Min offset for displaying usage (default: 20)
Definition: argList.H:169
Non-intrusive singly-linked list.
void printUsage() const
Print usage.
Definition: argList.C:1092
static string::size_type usageMax
Max screen width for displaying usage (default: 80)
Definition: argList.H:172
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:157
Signal handler for SEGV interupt.
Definition: sigSegv.H:54
const string & option(const word &opt) const
Return the argument string associated with the named option.
Definition: argListI.H:102
Namespace for OpenFOAM.
const stringList & args() const
Return arguments.
Definition: argListI.H:66
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:114