1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
28 Extract command arguments and options from the supplied
29 \a argc and @a argv parameters.
31 Sequences with "(" ... ")" are transformed into a stringList.
34 program -listFiles \( *.txt \)
36 would create a stringList:
38 ( "file1.txt" "file2.txt" ... "fileN.txt" )
40 The backslash-escaping is required to avoid interpretation by the shell.
42 \par Default command-line options
43 \param -case \<dir\> \n
44 select an case directory instead of the current working directory
46 specify case as a parallel job
48 display the documentation in browser
50 display the source documentation in browser
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
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.
73 \*---------------------------------------------------------------------------*/
78 #include "stringList.H"
81 #include "HashTable.H"
85 #include "IStringStream.H"
86 #include "OSspecific.H"
93 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
98 /*---------------------------------------------------------------------------*\
99 Class argList Declaration
100 \*---------------------------------------------------------------------------*/
105 static bool bannerEnabled;
108 HashTable<string> options_;
112 fileName globalCase_;
115 ParRunControl parRunControl_;
124 // Private Member Functions
126 //- Helper function for printUsage
127 static void printOptionUsage
129 const label location,
133 //- get rootPath_ / globalCase_ from one of the following forms
137 // Also export FOAM_CASE and FOAM_CASENAME environment variables
138 // so they can be used immediately (eg, in decomposeParDict)
141 //- Transcribe argv into internal args_
142 // return true if any "(" ... ")" sequences were captured
143 bool regroupArgv(int& argc, char**& argv);
148 // Static data members
150 //- A list of valid (mandatory) arguments
151 static SLList<string> validArgs;
153 //- A list of valid options
154 static HashTable<string> validOptions;
156 //- A list of valid parallel options
157 static HashTable<string> validParOptions;
159 //- Short usage information for validOptions
160 static HashTable<string> optionUsage;
162 //- Additional notes for usage
163 static SLList<string> notes;
165 //- Min offset for displaying usage (default: 20)
166 static string::size_type usageMin;
168 //- Max screen width for displaying usage (default: 80)
169 static string::size_type usageMax;
171 //! \cond internalClass
172 class initValidTables
183 //- Construct from argc and argv
184 // checking the arguments and options as requested
202 //- Name of executable without the path
203 inline const word& executable() const;
206 inline const fileName& rootPath() const;
208 //- Return case name (parallel run) or global case (serial run)
209 inline const fileName& caseName() const;
212 inline const fileName& globalCaseName() const;
214 //- Return the path to the caseName
215 inline fileName path() const;
218 inline const stringList& args() const;
220 //- Return the argument corresponding to index.
221 inline const string& arg(const label index) const;
223 //- Return the number of arguments
224 inline label size() const;
226 //- Read a value from the argument at index.
227 // Index 0 corresponds to the name of the executable.
228 // Index 1 corresponds to the first argument.
230 inline T argRead(const label index) const;
232 //- Return arguments that are additional to the executable
233 // \deprecated use operator[] directly (deprecated Feb 2010)
234 stringList::subList additionalArgs() const
236 return stringList::subList(args_, args_.size()-1, 1);
241 inline const Foam::HashTable<string>& options() const;
243 //- Return the argument string associated with the named option
244 inline const string& option(const word& opt) const;
246 //- Return true if the named option is found
247 inline bool optionFound(const word& opt) const;
249 //- Return an IStringStream from the named option
250 inline IStringStream optionLookup(const word& opt) const;
252 //- Read a value from the named option
254 inline T optionRead(const word& opt) const;
256 //- Read a value from the named option if present.
257 // Return true if the named option was found.
259 inline bool optionReadIfPresent(const word& opt, T&) const;
261 //- Read a value from the named option if present.
262 // Return true if the named option was found, otherwise
263 // use the supplied default and return false.
265 inline bool optionReadIfPresent
272 //- Read a value from the named option if present.
273 // Return true if the named option was found.
275 inline T optionLookupOrDefault
281 //- Read a List of values from the named option
283 List<T> optionReadList(const word& opt) const
285 return readList<T>(optionLookup(opt)());
289 //- Return the argument corresponding to index.
290 // Index 0 corresponds to the name of the executable.
291 // Index 1 corresponds to the first argument.
292 inline const string& operator[](const label index) const;
294 //- Return the argument string associated with the named option
296 inline const string& operator[](const word& opt) const;
300 //- Add to a bool option to validOptions with usage information
301 static void addBoolOption
304 const string& usage = ""
307 //- Add to an option to validOptions with usage information
308 // An option with an empty param is a bool option
309 static void addOption
312 const string& param = "",
313 const string& usage = ""
316 //- Add option usage information to optionUsage
323 //- Add extra notes for the usage information
324 // This string is used "as-is" without additional formatting
325 static void addNote(const string&);
327 //- Remove option from validOptions and from optionUsage
328 static void removeOption(const word& opt);
330 //- Disable emitting the banner information
331 static void noBanner();
333 //- Remove the parallel options
334 static void noParallel();
337 //- Set option directly (use with caution)
338 // An option with an empty param is a bool option.
339 // Not all valid options can also be set: eg, -case, -roots, ...
340 // Return true if the existing option value needed changing,
341 // or if the option did not previously exist.
342 bool setOption(const word& opt, const string& param = "");
344 //- Unset option directly (use with caution)
345 // Not all valid options can also be unset: eg, -case, -roots ...
346 // Return true if the option existed before being unset.
347 bool unsetOption(const word& opt);
352 //- Print notes (if any)
353 void printNotes() const;
356 void printUsage() const;
358 //- Display documentation in browser
359 // Optionally display the application source code
360 void displayDoc(bool source=false) const;
365 //- Check argument list
366 bool check(bool checkArgs=true, bool checkOpts=true) const;
368 //- Check root path and case path
369 bool checkRootCase() const;
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
375 } // End namespace Foam
377 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
379 #include "argListI.H"
381 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
385 // ************************************************************************* //