argList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 
107  //- Switch on/off parallel mode. Has to be first to be constructed
108  // so destructor is done last.
109  ParRunControl parRunControl_;
110 
111  stringList args_;
112  HashTable<string> options_;
113 
114  word executable_;
115  fileName rootPath_;
116  fileName globalCase_;
117  fileName case_;
118  string argListStr_;
119 
120  // Signal handlers
121  sigFpe sigFpe_;
122  sigInt sigInt_;
123  sigQuit sigQuit_;
124  sigSegv sigSegv_;
125 
126 
127  // Private Member Functions
128 
129  //- Helper function for printUsage
130  static void printOptionUsage
131  (
132  const label location,
133  const string& str
134  );
135 
136  //- Get rootPath_ / globalCase_ from one of the following forms
137  // * [-case dir]
138  // * cwd
139  //
140  // Also export FOAM_CASE and FOAM_CASENAME environment variables
141  // so they can be used immediately (eg, in decomposeParDict)
142  void getRootCase();
143 
144  //- Transcribe argv into internal args_
145  // return true if any "(" ... ")" sequences were captured
146  bool regroupArgv(int& argc, char**& argv);
147 
148 
149 public:
150 
151  // Static Data Members
152 
153  //- A list of valid (mandatory) arguments
154  static SLList<string> validArgs;
155 
156  //- A list of valid options
158 
159  //- A list of valid parallel options
161 
162  //- Short usage information for validOptions
164 
165  //- Additional notes for usage
166  static SLList<string> notes;
167 
168  //- Min offset for displaying usage (default: 20)
170 
171  //- Max screen width for displaying usage (default: 80)
173 
174  //- Standard name for the post-processing option
176 
177  // Class to initialize options table
178  // with the standard case related options
179  class initValidTables
180  {
181  public:
182 
183  initValidTables();
184 
185  //- Clear the options table
186  // for utilities which do not operate on files rather than cases
187  static void clear();
188  };
189 
190 
191  // Constructors
192 
193  //- Construct from argc and argv
194  // checking the arguments and options as requested
195  argList
196  (
197  int& argc,
198  char**& argv,
199  bool checkArgs = true,
200  bool checkOpts = true,
201  bool initialise = true
202  );
203 
204  //- Construct copy with new options
205  argList
206  (
207  const argList& args,
208  const HashTable<string>& options,
209  bool checkArgs = true,
210  bool checkOpts = true,
211  bool initialise = true
212  );
213 
214 
215  //- Destructor
216  virtual ~argList();
217 
218 
219  // Member Functions
220 
221  //- Parse
222  void parse
223  (
224  bool checkArgs,
225  bool checkOpts,
226  bool initialise
227  );
228 
229 
230  // Access
231 
232  //- Name of executable without the path
233  inline const word& executable() const;
234 
235  //- Return root path
236  inline const fileName& rootPath() const;
237 
238  //- Return case name (parallel run) or global case (serial run)
239  inline const fileName& caseName() const;
240 
241  //- Return case name
242  inline const fileName& globalCaseName() const;
243 
244  //- Return parRunControl
245  inline const ParRunControl& parRunControl() const;
246 
247  //- Return the path to the caseName
248  inline fileName path() const;
249 
250  //- Return arguments
251  inline const stringList& args() const;
252 
253  //- Return non-const access to arguments
254  inline stringList& args();
255 
256  //- Return the argument corresponding to index.
257  inline const string& arg(const label index) const;
258 
259  //- Return the number of arguments
260  inline label size() const;
261 
262  //- Read a value from the argument at index.
263  // Index 0 corresponds to the name of the executable.
264  // Index 1 corresponds to the first argument.
265  template<class T>
266  inline T argRead(const label index) const;
267 
268  //- Return options
269  inline const Foam::HashTable<string>& options() const;
270 
271  //- Return non-const access to options
273 
274  //- Return the argument string associated with the named option
275  inline const string& option(const word& opt) const;
276 
277  //- Return true if the named option is found
278  inline bool optionFound(const word& opt) const;
279 
280  //- Return an IStringStream from the named option
281  inline IStringStream optionLookup(const word& opt) const;
282 
283  //- Read a value from the named option
284  template<class T>
285  inline T optionRead(const word& opt) const;
286 
287  //- Read a value from the named option if present.
288  // Return true if the named option was found.
289  template<class T>
290  inline bool optionReadIfPresent(const word& opt, T&) const;
291 
292  //- Read a value from the named option if present.
293  // Return true if the named option was found, otherwise
294  // use the supplied default and return false.
295  template<class T>
296  inline bool optionReadIfPresent
297  (
298  const word& opt,
299  T&,
300  const T& deflt
301  ) const;
302 
303  //- Read a value from the named option if present.
304  // Return supplied default otherwise.
305  template<class T>
306  inline T optionLookupOrDefault
307  (
308  const word& opt,
309  const T& deflt
310  ) const;
311 
312  //- Read a List of values from the named option
313  template<class T>
314  List<T> optionReadList(const word& opt) const
315  {
316  return readList<T>(optionLookup(opt)());
317  }
318 
319 
320  //- Return the argument corresponding to index.
321  // Index 0 corresponds to the name of the executable.
322  // Index 1 corresponds to the first argument.
323  inline const string& operator[](const label index) const;
324 
325  //- Return the argument string associated with the named option
326  // \sa option()
327  inline const string& operator[](const word& opt) const;
328 
329 
330  // Edit
331 
332  //- Add to a bool option to validOptions with usage information
333  static void addBoolOption
334  (
335  const word& opt,
336  const string& usage = ""
337  );
338 
339  //- Add to an option to validOptions with usage information
340  // An option with an empty param is a bool option
341  static void addOption
342  (
343  const word& opt,
344  const string& param = "",
345  const string& usage = ""
346  );
347 
348  //- Add option usage information to optionUsage
349  static void addUsage
350  (
351  const word& opt,
352  const string& usage
353  );
354 
355  //- Add extra notes for the usage information
356  // This string is used "as-is" without additional formatting
357  static void addNote(const string&);
358 
359  //- Remove option from validOptions and from optionUsage
360  static void removeOption(const word& opt);
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:1426
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:1105
static void removeOption(const word &opt)
Remove option from validOptions and from optionUsage.
Definition: argList.C:167
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:79
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:174
bool unsetOption(const word &opt)
Unset option directly (use with caution)
Definition: argList.C:1181
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:142
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:415
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:165
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:174
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
static bool postProcess(int argc, char *argv[])
Return true if the post-processing option is specified.
Definition: argList.C:283
virtual ~argList()
Destructor.
Definition: argList.C:1093
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:595
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 interrupt.
Definition: sigQuit.H:54
void displayDoc(bool source=false) const
Display documentation in browser.
Definition: argList.C:1308
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:159
void printNotes() const
Print notes (if any)
Definition: argList.C:1217
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:127
label size() const
Return the number of arguments.
Definition: argListI.H:84
static HashTable< string > optionUsage
Short usage information for validOptions.
Definition: argList.H:162
An STL-conforming hash table.
Definition: HashTable.H:61
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 interrupt.
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:313
static void clear()
Clear the options table.
Definition: argList.C:99
bool check(bool checkArgs=true, bool checkOpts=true) const
Check argument list.
Definition: argList.C:1385
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:117
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:158
static string::size_type usageMin
Min offset for displaying usage (default: 20)
Definition: argList.H:168
Non-intrusive singly-linked list.
void printUsage() const
Print usage.
Definition: argList.C:1231
static string::size_type usageMax
Max screen width for displaying usage (default: 80)
Definition: argList.H:171
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:156
Signal handler for SEGV interrupt.
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