|
|
|
argList.HGo to the documentation of this file.00001 /*---------------------------------------------------------------------------*\ 00002 ========= | 00003 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox 00004 \\ / O peration | 00005 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. 00006 \\/ M anipulation | 00007 ------------------------------------------------------------------------------- 00008 License 00009 This file is part of OpenFOAM. 00010 00011 OpenFOAM is free software; you can redistribute it and/or modify it 00012 under the terms of the GNU General Public License as published by the 00013 Free Software Foundation; either version 2 of the License, or (at your 00014 option) any later version. 00015 00016 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT 00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00018 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00019 for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with OpenFOAM; if not, write to the Free Software Foundation, 00023 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00024 00025 Class 00026 Foam::argList 00027 00028 Description 00029 Extract command arguments and options from the supplied 00030 @a argc and @a argv parameters. 00031 00032 Sequences with "(" ... ")" are transformed into a stringList. 00033 For example, 00034 @verbatim 00035 program -listFiles \( *.txt \) 00036 @endverbatim 00037 would create a stringList: 00038 @verbatim 00039 ( "file1.txt" "file2.txt" ... "fileN.txt" ) 00040 @endverbatim 00041 The backslash-escaping has been used to avoid shell expansions. 00042 00043 @par Default command-line options 00044 @param -case <dir> \n 00045 select an case directory instead of the current working directory 00046 @param -parallel \n 00047 specify case as a parallel job 00048 @param -doc \n 00049 display the documentation in browser 00050 @param -srcDoc \n 00051 display the source documentation in browser 00052 @param -help \n 00053 print the usage 00054 00055 The environment variable @b FOAM_CASE is set to the path of the 00056 global case (same for serial and parallel jobs). 00057 The environment variable @b FOAM_CASENAME is set to the name of the 00058 global case. 00059 00060 Note 00061 - Adjustment of the valid (mandatory) arguments 00062 by directly manipulating the static member argList::validArgs. 00063 - Adjustment of the valid options 00064 by directly manipulating the static member argList::validOptions. 00065 00066 SourceFiles 00067 argList.C 00068 00069 \*---------------------------------------------------------------------------*/ 00070 00071 #ifndef argList_H 00072 #define argList_H 00073 00074 #include "stringList.H" 00075 #include "SubList.H" 00076 #include "SLList.H" 00077 #include "HashTable.H" 00078 #include "word.H" 00079 #include "fileName.H" 00080 #include "parRun.H" 00081 #include "IStringStream.H" 00082 00083 #include "sigFpe.H" 00084 #include "sigInt.H" 00085 #include "sigQuit.H" 00086 #include "sigSegv.H" 00087 00088 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00089 00090 namespace Foam 00091 { 00092 00093 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00094 00095 class argList 00096 { 00097 // Private data 00098 static bool bannerEnabled; 00099 00100 stringList args_; 00101 HashTable<string> options_; 00102 00103 word executable_; 00104 fileName rootPath_; 00105 fileName globalCase_; 00106 fileName case_; 00107 00108 ParRunControl parRunControl_; 00109 00110 // Signal handlers 00111 sigFpe sigFpe_; 00112 sigInt sigInt_; 00113 sigQuit sigQuit_; 00114 sigSegv sigSegv_; 00115 00116 00117 // Private member functions 00118 00119 void getRootCase(); 00120 00121 //- Transcribe argv into internal args_ 00122 // return true if any "(" ... ")" sequences were captured 00123 bool regroupArgv(int& argc, char**& argv); 00124 00125 00126 public: 00127 00128 // Static data members 00129 00130 //- A list of valid (mandatory) arguments 00131 static SLList<string> validArgs; 00132 00133 //- A list of valid options 00134 static HashTable<string> validOptions; 00135 00136 //- A list of valid parallel options 00137 static HashTable<string> validParOptions; 00138 00140 class initValidTables 00141 { 00142 public: 00143 00144 initValidTables(); 00145 }; 00147 00148 00149 // Constructors 00150 00151 //- Construct from argc and argv 00152 // checking the arguments and options as requested 00153 argList 00154 ( 00155 int& argc, 00156 char**& argv, 00157 bool checkArgs=true, 00158 bool checkOpts=true 00159 ); 00160 00161 00162 // Destructor 00163 00164 virtual ~argList(); 00165 00166 00167 // Member functions 00168 00169 // Access 00170 00171 //- Name of executable 00172 const word& executable() const 00173 { 00174 return executable_; 00175 } 00176 00177 //- Return root path 00178 const fileName& rootPath() const 00179 { 00180 return rootPath_; 00181 } 00182 00183 //- Return case name 00184 const fileName& globalCaseName() const 00185 { 00186 return globalCase_; 00187 } 00188 00189 //- Return case name (parallel run) or global case (serial run) 00190 const fileName& caseName() const 00191 { 00192 return case_; 00193 } 00194 00195 //- Return the path 00196 fileName path() const 00197 { 00198 return rootPath()/caseName(); 00199 } 00200 00201 //- Return arguments 00202 const stringList& args() const 00203 { 00204 return args_; 00205 } 00206 00207 //- Return additionl arguments, 00208 // i.e. those additional to the executable itself 00209 stringList::subList additionalArgs() const; 00210 00211 //- Return options 00212 const Foam::HashTable<string>& options() const 00213 { 00214 return options_; 00215 } 00216 00217 //- Return the argument string associated with the named option 00218 const string& option(const word& opt) const 00219 { 00220 return options_.operator[](opt); 00221 } 00222 00223 //- Return true if the named option is found 00224 bool optionFound(const word& opt) const 00225 { 00226 return options_.found(opt); 00227 } 00228 00229 //- Return an IStringStream to the named option 00230 IStringStream optionLookup(const word& opt) const 00231 { 00232 return IStringStream(option(opt)); 00233 } 00234 00235 //- Read a value from the named option 00236 template<class T> 00237 T optionRead(const word& opt) const 00238 { 00239 T val; 00240 optionLookup(opt)() >> val; 00241 return val; 00242 } 00243 00244 //- Read a value from the named option if present. 00245 // Return true if the named option was found. 00246 template<class T> 00247 bool optionReadIfPresent(const word& opt, T& val) const 00248 { 00249 if (optionFound(opt)) 00250 { 00251 optionLookup(opt)() >> val; 00252 return true; 00253 } 00254 else 00255 { 00256 return false; 00257 } 00258 } 00259 00260 //- Read a List of values from the named option 00261 template<class T> 00262 List<T> optionReadList(const word& opt) const 00263 { 00264 return readList<T>(optionLookup(opt)()); 00265 } 00266 00267 00268 // Edit 00269 00270 //- Disable emitting the banner information 00271 static void noBanner(); 00272 00273 //- Remove the parallel options 00274 static void noParallel(); 00275 00276 00277 // Print 00278 00279 //- Print usage 00280 void printUsage() const; 00281 00282 //- Display documentation in browser 00283 // Optionally display the application source code 00284 void displayDoc(bool source=false) const; 00285 00286 00287 // Check 00288 00289 //- Check argument list 00290 bool check(bool checkArgs=true, bool checkOpts=true) const; 00291 00292 //- Check root path and case path 00293 bool checkRootCase() const; 00294 }; 00295 00296 00297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00298 00299 } // End namespace Foam 00300 00301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00302 00303 #endif 00304 00305 // ************************************************************************* // |