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-2015 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  \par Default command-line options
43  \param -case <dir> \n
44  select a 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  - 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
90 
91 namespace Foam
92 {
93 
94 /*---------------------------------------------------------------------------*\
95  Class argList Declaration
96 \*---------------------------------------------------------------------------*/
97 
98 class argList
99 {
100  // Private data
101  static bool bannerEnabled;
102 
103  //- Switch on/off parallel mode. Has to be first to be constructed
104  // so destructor is done last.
106 
109 
114  string argListStr_;
115 
116 
117  // Private Member Functions
118 
119  //- Helper function for printUsage
120  static void printOptionUsage
121  (
122  const label location,
123  const string& str
124  );
125 
126  //- Get rootPath_ / globalCase_ from one of the following forms
127  // * [-case dir]
128  // * cwd
129  //
130  // Also export FOAM_CASE and FOAM_CASENAME environment variables
131  // so they can be used immediately (eg, in decomposeParDict)
132  void getRootCase();
133 
134  //- Transcribe argv into internal args_
135  // return true if any "(" ... ")" sequences were captured
136  bool regroupArgv(int& argc, char**& argv);
137 
138 
139 public:
140 
141  // Static data members
142 
143  //- A list of valid (mandatory) arguments
144  static SLList<string> validArgs;
145 
146  //- A list of valid options
148 
149  //- A list of valid parallel options
151 
152  //- Short usage information for validOptions
154 
155  //- Additional notes for usage
156  static SLList<string> notes;
157 
158  //- Min offset for displaying usage (default: 20)
160 
161  //- Max screen width for displaying usage (default: 80)
163 
165  class initValidTables
166  {
167  public:
168 
169  initValidTables();
170  };
172 
173 
174  // Constructors
175 
176  //- Construct from argc and argv
177  // checking the arguments and options as requested
178  argList
179  (
180  int& argc,
181  char**& argv,
182  bool checkArgs = true,
183  bool checkOpts = true,
184  bool initialise = true
185  );
186 
187  //- Construct copy with new options
188  argList
189  (
190  const argList& args,
191  const HashTable<string>& options,
192  bool checkArgs = true,
193  bool checkOpts = true,
194  bool initialise = true
195  );
196 
197 
198  //- Destructor
199  virtual ~argList();
200 
201 
202  // Member functions
203 
204  //- Parse
205  void parse
206  (
207  bool checkArgs,
208  bool checkOpts,
209  bool initialise
210  );
211 
212 
213  // Access
214 
215  //- Name of executable without the path
216  inline const word& executable() const;
217 
218  //- Return root path
219  inline const fileName& rootPath() const;
220 
221  //- Return case name (parallel run) or global case (serial run)
222  inline const fileName& caseName() const;
223 
224  //- Return case name
225  inline const fileName& globalCaseName() const;
226 
227  //- Return parRunControl
228  inline const ParRunControl& parRunControl() const;
229 
230  //- Return the path to the caseName
231  inline fileName path() const;
232 
233  //- Return arguments
234  inline const stringList& args() const;
235 
236  //- Return non-const access to arguments
237  inline stringList& args();
238 
239  //- Return the argument corresponding to index.
240  inline const string& arg(const label index) const;
241 
242  //- Return the number of arguments
243  inline label size() const;
244 
245  //- Read a value from the argument at index.
246  // Index 0 corresponds to the name of the executable.
247  // Index 1 corresponds to the first argument.
248  template<class T>
249  inline T argRead(const label index) const;
250 
251  //- Return arguments that are additional to the executable
252  // \deprecated use operator[] directly (deprecated Feb 2010)
254  {
255  return stringList::subList(args_, args_.size()-1, 1);
256  }
257 
258 
259  //- Return options
260  inline const Foam::HashTable<string>& options() const;
261 
262  //- Return non-const access to options
264 
265  //- Return the argument string associated with the named option
266  inline const string& option(const word& opt) const;
267 
268  //- Return true if the named option is found
269  inline bool optionFound(const word& opt) const;
270 
271  //- Return an IStringStream from the named option
272  inline IStringStream optionLookup(const word& opt) const;
273 
274  //- Read a value from the named option
275  template<class T>
276  inline T optionRead(const word& opt) const;
277 
278  //- Read a value from the named option if present.
279  // Return true if the named option was found.
280  template<class T>
281  inline bool optionReadIfPresent(const word& opt, T&) const;
282 
283  //- Read a value from the named option if present.
284  // Return true if the named option was found, otherwise
285  // use the supplied default and return false.
286  template<class T>
287  inline bool optionReadIfPresent
288  (
289  const word& opt,
290  T&,
291  const T& deflt
292  ) const;
293 
294  //- Read a value from the named option if present.
295  // Return supplied default otherwise.
296  template<class T>
297  inline T optionLookupOrDefault
298  (
299  const word& opt,
300  const T& deflt
301  ) const;
302 
303  //- Read a List of values from the named option
304  template<class T>
305  List<T> optionReadList(const word& opt) const
306  {
307  return readList<T>(optionLookup(opt)());
308  }
309 
310 
311  //- Return the argument corresponding to index.
312  // Index 0 corresponds to the name of the executable.
313  // Index 1 corresponds to the first argument.
314  inline const string& operator[](const label index) const;
315 
316  //- Return the argument string associated with the named option
317  // \sa option()
318  inline const string& operator[](const word& opt) const;
319 
320 
321  // Edit
322 
323  //- Add to a bool option to validOptions with usage information
324  static void addBoolOption
325  (
326  const word& opt,
327  const string& usage = ""
328  );
329 
330  //- Add to an option to validOptions with usage information
331  // An option with an empty param is a bool option
332  static void addOption
333  (
334  const word& opt,
335  const string& param = "",
336  const string& usage = ""
337  );
338 
339  //- Add option usage information to optionUsage
340  static void addUsage
341  (
342  const word& opt,
343  const string& usage
344  );
345 
346  //- Add extra notes for the usage information
347  // This string is used "as-is" without additional formatting
348  static void addNote(const string&);
349 
350  //- Remove option from validOptions and from optionUsage
351  static void removeOption(const word& opt);
352 
353  //- Disable emitting the banner information
354  static void noBanner();
355 
356  //- Remove the parallel options
357  static void noParallel();
358 
359 
360  //- Set option directly (use with caution)
361  // An option with an empty param is a bool option.
362  // Not all valid options can also be set: eg, -case, -roots, ...
363  // Return true if the existing option value needed changing,
364  // or if the option did not previously exist.
365  bool setOption(const word& opt, const string& param = "");
366 
367  //- Unset option directly (use with caution)
368  // Not all valid options can also be unset: eg, -case, -roots ...
369  // Return true if the option existed before being unset.
370  bool unsetOption(const word& opt);
371 
372 
373  // Print
374 
375  //- Print notes (if any)
376  void printNotes() const;
377 
378  //- Print usage
379  void printUsage() const;
380 
381  //- Display documentation in browser
382  // Optionally display the application source code
383  void displayDoc(bool source=false) const;
384 
385 
386  // Check
387 
388  //- Check argument list
389  bool check(bool checkArgs=true, bool checkOpts=true) const;
390 
391  //- Check root path and case path
392  bool checkRootCase() const;
393 };
394 
395 
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397 
398 } // End namespace Foam
399 
400 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401 
402 #include "argListI.H"
403 
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 
406 #endif
407 
408 // ************************************************************************* //
Foam::argList::validArgs
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:143
Foam::argList::options_
HashTable< string > options_
Definition: argList.H:107
Foam::argList::noBanner
static void noBanner()
Disable emitting the banner information.
Definition: argList.C:155
parRun.H
Foam::argList::argListStr_
string argListStr_
Definition: argList.H:113
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
SubList.H
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::argList::args
const stringList & args() const
Return arguments.
Definition: argListI.H:66
Foam::argList::args_
stringList args_
Definition: argList.H:106
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
HashTable.H
Foam::argList::addOption
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:108
Foam::argList::addNote
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:139
Foam::SLList
Non-intrusive singly-linked list.
Definition: SLList.H:47
Foam::argList::regroupArgv
bool regroupArgv(int &argc, char **&argv)
Transcribe argv into internal args_.
Definition: argList.C:274
Foam::argList::usageMin
static string::size_type usageMin
Min offset for displaying usage (default: 20)
Definition: argList.H:158
Foam::argList::size
label size() const
Return the number of arguments.
Definition: argListI.H:84
Foam::argList::optionUsage
static HashTable< string > optionUsage
Short usage information for validOptions.
Definition: argList.H:152
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::argList::executable_
word executable_
Definition: argList.H:109
Foam::argList::arg
const string & arg(const label index) const
Return the argument corresponding to index.
Definition: argListI.H:78
Foam::argList::addBoolOption
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:98
Foam::argList::bannerEnabled
static bool bannerEnabled
Definition: argList.H:100
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:97
Foam::argList::rootPath_
fileName rootPath_
Definition: argList.H:110
Foam::argList::rootPath
const fileName & rootPath() const
Return root path.
Definition: argListI.H:36
IStringStream.H
Foam::argList::parRunControl
const ParRunControl & parRunControl() const
Return parRunControl.
Definition: argListI.H:54
Foam::argList::addUsage
static void addUsage(const word &opt, const string &usage)
Add option usage information to optionUsage.
Definition: argList.C:123
Foam::argList::validParOptions
static HashTable< string > validParOptions
A list of valid parallel options.
Definition: argList.H:149
Foam::argList::optionReadList
List< T > optionReadList(const word &opt) const
Read a List of values from the named option.
Definition: argList.H:304
Foam::argList::printNotes
void printNotes() const
Print notes (if any)
Definition: argList.C:1031
Foam::argList::parse
void parse(bool checkArgs, bool checkOpts, bool initialise)
Parse.
Definition: argList.C:506
Foam::argList::options
const Foam::HashTable< string > & options() const
Return options.
Definition: argListI.H:90
Foam::argList::executable
const word & executable() const
Name of executable without the path.
Definition: argListI.H:30
Foam::label
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
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
fileName.H
Foam::argList::setOption
bool setOption(const word &opt, const string &param="")
Set option directly (use with caution)
Definition: argList.C:921
Foam::argList::getRootCase
void getRootCase()
Get rootPath_ / globalCase_ from one of the following forms.
Definition: argList.C:330
Foam::argList::printUsage
void printUsage() const
Print usage.
Definition: argList.C:1045
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
Foam::argList::printOptionUsage
static void printOptionUsage(const label location, const string &str)
Helper function for printUsage.
Definition: argList.C:171
Foam::argList::case_
fileName case_
Definition: argList.H:112
argListI.H
Foam::argList::option
const string & option(const word &opt) const
Return the argument string associated with the named option.
Definition: argListI.H:102
Foam::argList::optionLookupOrDefault
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
Foam::argList::path
fileName path() const
Return the path to the caseName.
Definition: argListI.H:60
Foam::List::subList
SubList< T > subList
Declare type of subList.
Definition: List.H:153
Foam::argList::validOptions
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:146
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::argList::globalCase_
fileName globalCase_
Definition: argList.H:111
Foam::HashTable
An STL-conforming hash table.
Definition: HashTable.H:61
Foam::argList::unsetOption
bool unsetOption(const word &opt)
Unset option directly (use with caution)
Definition: argList.C:996
Foam::argList::parRunControl_
ParRunControl parRunControl_
Switch on/off parallel mode. Has to be first to be constructed.
Definition: argList.H:104
SLList.H
Foam::argList::usageMax
static string::size_type usageMax
Max screen width for displaying usage (default: 80)
Definition: argList.H:161
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::argList::optionRead
T optionRead(const word &opt) const
Read a value from the named option.
Definition: argListI.H:187
Foam::argList::notes
static SLList< string > notes
Additional notes for usage.
Definition: argList.H:155
Foam::argList::checkRootCase
bool checkRootCase() const
Check root path and case path.
Definition: argList.C:1225
Foam::argList::removeOption
static void removeOption(const word &opt)
Remove option from validOptions and from optionUsage.
Definition: argList.C:148
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Foam::ParRunControl
Helper class for initializing parallel jobs from the command arguments.
Definition: parRun.H:46
Foam::argList::additionalArgs
stringList::subList additionalArgs() const
Return arguments that are additional to the executable.
Definition: argList.H:252
Foam::argList::argRead
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:177
Foam::argList::caseName
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:42
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
word.H
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:161
Foam::argList::operator[]
const string & operator[](const label index) const
Return the argument corresponding to index.
Definition: argListI.H:255
Foam::argList::optionReadIfPresent
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
stringList.H
Foam::argList::globalCaseName
const fileName & globalCaseName() const
Return case name.
Definition: argListI.H:48
Foam::argList::~argList
virtual ~argList()
Destructor.
Definition: argList.C:913
Foam::argList::check
bool check(bool checkArgs=true, bool checkOpts=true) const
Check argument list.
Definition: argList.C:1184
Foam::argList::displayDoc
void displayDoc(bool source=false) const
Display documentation in browser.
Definition: argList.C:1124
Foam::argList::optionLookup
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:114
Foam::argList::argList
argList(int &argc, char **&argv, bool checkArgs=true, bool checkOpts=true, bool initialise=true)
Construct from argc and argv.
Definition: argList.C:388