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