Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / global / argList / argList.H
blob77f9d07de6ac53f7b468ef26830084a4b4fa7efa
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Class
25     Foam::argList
27 Description
28     Extract command arguments and options from the supplied
29     @a argc and @a argv parameters.
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.
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
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.
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.
65 SourceFiles
66     argList.C
68 \*---------------------------------------------------------------------------*/
70 #ifndef argList_H
71 #define argList_H
73 #include "stringList.H"
74 #include "SubList.H"
75 #include "SLList.H"
76 #include "HashTable.H"
77 #include "word.H"
78 #include "fileName.H"
79 #include "parRun.H"
80 #include "IStringStream.H"
82 #include "sigFpe.H"
83 #include "sigInt.H"
84 #include "sigQuit.H"
85 #include "sigSegv.H"
87 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
89 namespace Foam
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
94 class argList
96     // Private data
97         static bool bannerEnabled;
99         stringList args_;
100         HashTable<string> options_;
102         word executable_;
103         fileName rootPath_;
104         fileName globalCase_;
105         fileName case_;
107         ParRunControl parRunControl_;
109         // Signal handlers
110         sigFpe sigFpe_;
111         sigInt sigInt_;
112         sigQuit sigQuit_;
113         sigSegv sigSegv_;
116     // Private member functions
118         void getRootCase();
120         //- Transcribe argv into internal args_
121         //  return true if any "(" ... ")" sequences were captured
122         bool regroupArgv(int& argc, char**& argv);
125 public:
127     // Static data members
129         //- A list of valid (mandatory) arguments
130         static SLList<string> validArgs;
132         //- A list of valid options
133         static HashTable<string> validOptions;
135         //- A list of valid parallel options
136         static HashTable<string> validParOptions;
138         //- Name of the application-specific dictionary
139         static word appDictName_;
141         //! @cond ignoreDocumentation
142         class initValidTables
143         {
144         public:
146             initValidTables();
147         };
148         //! @endcond ignoreDocumentation
151     // Constructors
153         //- Construct from argc and argv
154         //  checking the arguments and options as requested
155         argList
156         (
157             int& argc,
158             char**& argv,
159             bool checkArgs=true,
160             bool checkOpts=true
161         );
164     // Destructor
166         virtual ~argList();
169     // Member functions
171         // Access
173             //- Name of executable
174             const word& executable() const
175             {
176                 return executable_;
177             }
179             //- Return root path
180             const fileName& rootPath() const
181             {
182                 return rootPath_;
183             }
185             //- Return case name
186             const fileName& globalCaseName() const
187             {
188                 return globalCase_;
189             }
191             //- Return case name (parallel run) or global case (serial run)
192             const fileName& caseName() const
193             {
194                 return case_;
195             }
197             //- Return the path
198             fileName path() const
199             {
200                 return rootPath()/caseName();
201             }
203             //- Return arguments
204             const stringList& args() const
205             {
206                 return args_;
207             }
209             //- Return additionl arguments,
210             //  i.e. those additional to the executable itself
211             stringList::subList additionalArgs() const;
213             //- Return options
214             const Foam::HashTable<string>& options() const
215             {
216                 return options_;
217             }
219             //- Return the argument string associated with the named option
220             const string& option(const word& opt) const
221             {
222                 return options_.operator[](opt);
223             }
225             //- Return true if the named option is found
226             bool optionFound(const word& opt) const
227             {
228                 return options_.found(opt);
229             }
231             //- Return an IStringStream to the named option
232             IStringStream optionLookup(const word& opt) const
233             {
234                 return IStringStream(option(opt));
235             }
237             //- Read a value from the named option
238             template<class T>
239             T optionRead(const word& opt) const
240             {
241                 T val;
242                 optionLookup(opt)() >> val;
243                 return val;
244             }
246             //- Read a value from the named option if present.
247             //  Return true if the named option was found.
248             template<class T>
249             bool optionReadIfPresent(const word& opt, T& val) const
250             {
251                 if (optionFound(opt))
252                 {
253                     optionLookup(opt)() >> val;
254                     return true;
255                 }
256                 else
257                 {
258                     return false;
259                 }
260             }
262             //- Read a List of values from the named option
263             template<class T>
264             List<T> optionReadList(const word& opt) const
265             {
266                 return readList<T>(optionLookup(opt)());
267             }
269             //- Return appDictName_
270             const word appDictName() const
271             {
272                 return appDictName_;
273             }
275         // Edit
277             //- Disable emitting the banner information
278             static void noBanner();
280             //- Remove the parallel options
281             static void noParallel();
284         // Print
286             //- Print usage
287             void printUsage() const;
289             //- Display documentation in browser
290             //  Optionally display the application source code
291             void displayDoc(bool source=false) const;
294         // Check
296             //- Check argument list
297             bool check(bool checkArgs=true, bool checkOpts=true) const;
299             //- Check root path and case path
300             bool checkRootCase() const;
304 // Allow overriding the application-specific dictionary using the command-line
305 // parameter -appDict. The dictionary file must still be located in the case's
306 // "system" directory, or accessible using a path relative to the case's
307 // "system" directory.
308 // Example:
309 //    cellSet                                     # Default will be cellSetDict
310 //    cellSet -appDict cellSetDict                # Same as above
311 //    cellSet -appDict cellSetDict.rotorOnly
313 #define  AppSpecificDictionary(DefaultAppDict)                            \
314     argList::appDictName_ = (DefaultAppDict);                             \
315     argList::validOptions.insert("appDict", argList::appDictName_);
317 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
319 } // End namespace Foam
321 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
323 #endif
325 // ************************************************************************* //