Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // This example is provided by the Geant4-DNA 26 // This example is provided by the Geant4-DNA collaboration 27 // Any report or published results obtained us 27 // Any report or published results obtained using the Geant4-DNA software 28 // shall cite the following Geant4-DNA collabo 28 // shall cite the following Geant4-DNA collaboration publication: 29 // Med. Phys. 37 (2010) 4692-4708 29 // Med. Phys. 37 (2010) 4692-4708 30 // J. Comput. Phys. 274 (2014) 841-882 30 // J. Comput. Phys. 274 (2014) 841-882 31 // The Geant4-DNA web site is available at htt 31 // The Geant4-DNA web site is available at http://geant4-dna.org 32 // 32 // 33 // Author: Mathieu Karamitros 33 // Author: Mathieu Karamitros 34 // 34 // 35 // 35 // 36 /// \file CommandLineParser.cc 36 /// \file CommandLineParser.cc 37 /// \brief Implementation of the CommandLinePa 37 /// \brief Implementation of the CommandLineParser class 38 38 39 #include "CommandLineParser.hh" << 40 << 41 #include <iomanip> 39 #include <iomanip> >> 40 #include "CommandLineParser.hh" 42 41 43 using namespace std; 42 using namespace std; 44 using namespace G4DNAPARSER; 43 using namespace G4DNAPARSER; 45 44 46 CommandLineParser* CommandLineParser::fpInstan 45 CommandLineParser* CommandLineParser::fpInstance(0); 47 G4String Command::fNoOption = "NoOption"; 46 G4String Command::fNoOption = "NoOption"; 48 47 >> 48 49 //....oooOO0OOooo........oooOO0OOooo........oo 49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 50 50 51 inline bool MATCH(const char* a, const char* b << 51 inline bool MATCH(const char *a, const char *b) 52 { 52 { 53 return strcmp(a, b) == 0; 53 return strcmp(a, b) == 0; 54 } 54 } 55 55 56 //....oooOO0OOooo........oooOO0OOooo........oo 56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 57 57 58 CommandLineParser::CommandLineParser() 58 CommandLineParser::CommandLineParser() 59 { 59 { 60 // G4cout << "############ NEW PARSE ####### 60 // G4cout << "############ NEW PARSE ##########" << G4endl; 61 fpInstance = this; 61 fpInstance = this; 62 fOptionsWereSetup = false; 62 fOptionsWereSetup = false; 63 fMaxMarkerLength = 0; 63 fMaxMarkerLength = 0; 64 fMaxOptionNameLength = 0; 64 fMaxOptionNameLength = 0; 65 AddCommand("--help", Command::WithoutOption, 65 AddCommand("--help", Command::WithoutOption, "Print this help"); 66 AddCommand("-h", Command::WithoutOption, "Pr 66 AddCommand("-h", Command::WithoutOption, "Print this help"); 67 AddCommand("&", Command::WithoutOption); 67 AddCommand("&", Command::WithoutOption); 68 68 69 fVerbose = 0; 69 fVerbose = 0; 70 } 70 } 71 71 72 //....oooOO0OOooo........oooOO0OOooo........oo 72 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 73 73 74 CommandLineParser* CommandLineParser::GetParse 74 CommandLineParser* CommandLineParser::GetParser() 75 { 75 { 76 if (!fpInstance) new CommandLineParser; 76 if (!fpInstance) new CommandLineParser; 77 return fpInstance; 77 return fpInstance; 78 } 78 } 79 79 80 //....oooOO0OOooo........oooOO0OOooo........oo 80 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 81 81 82 CommandLineParser::~CommandLineParser() 82 CommandLineParser::~CommandLineParser() 83 { 83 { 84 std::map<G4String, Command*>::iterator it = 84 std::map<G4String, Command*>::iterator it = fCommandMap.begin(); 85 for (; it != fCommandMap.end(); it++) { << 85 for (; it != fCommandMap.end(); it++) >> 86 { 86 if (it->second) delete it->second; 87 if (it->second) delete it->second; 87 } 88 } 88 } 89 } 89 90 90 //....oooOO0OOooo........oooOO0OOooo........oo 91 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 91 92 92 void CommandLineParser::DeleteInstance() 93 void CommandLineParser::DeleteInstance() 93 { 94 { 94 if (fpInstance) { << 95 if (fpInstance) >> 96 { 95 delete fpInstance; 97 delete fpInstance; 96 fpInstance = 0; 98 fpInstance = 0; 97 } 99 } 98 } 100 } 99 101 100 //....oooOO0OOooo........oooOO0OOooo........oo 102 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 101 103 102 Command::Command(Command::Type commandType, co << 104 Command::Command(Command::Type commandType, >> 105 const G4String& description) 103 { 106 { 104 fType = commandType; 107 fType = commandType; 105 fDescription = description; 108 fDescription = description; 106 fActive = false; 109 fActive = false; 107 } 110 } 108 111 109 CommandWithOption::CommandWithOption(Command:: << 112 CommandWithOption::CommandWithOption(Command::Type commandType, 110 const G4S << 113 const G4String& description, 111 : Command(commandType, description) << 114 const G4String& defaultOption, >> 115 const G4String& optionName) : >> 116 Command(commandType, description) 112 { 117 { 113 fDefaultOption = defaultOption; 118 fDefaultOption = defaultOption; 114 fOptionName = optionName; 119 fOptionName = optionName; 115 fOption = ""; 120 fOption = ""; 116 } 121 } 117 122 >> 123 118 //....oooOO0OOooo........oooOO0OOooo........oo 124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 119 125 120 int CommandLineParser::Parse(int& argc, char** << 126 int CommandLineParser::Parse(int& argc, char **argv) 121 { 127 { 122 // G4cout << "Parse " << G4endl; 128 // G4cout << "Parse " << G4endl; 123 static char null[1] = {""}; << 129 static char null[1] = { "" }; 124 int firstArgc = argc; 130 int firstArgc = argc; 125 131 126 for (int i = 1; i < firstArgc; i++) { << 132 for (int i = 1; i < firstArgc; i++) >> 133 { 127 Command* command = FindCommand(argv[i]); 134 Command* command = FindCommand(argv[i]); 128 if (command == 0) continue; 135 if (command == 0) continue; 129 136 130 if (fVerbose) G4cout << "Command : " << ar 137 if (fVerbose) G4cout << "Command : " << argv[i] << G4endl; 131 138 132 fOptionsWereSetup = true; 139 fOptionsWereSetup = true; 133 command->fActive = true; 140 command->fActive = true; 134 141 135 G4String marker(argv[i]); 142 G4String marker(argv[i]); 136 143 137 if (strcmp(argv[i], "-h") != 0 && strcmp(a << 144 if (strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "--help") != 0) >> 145 { 138 argv[i] = null; 146 argv[i] = null; 139 } 147 } 140 148 141 if (command->fType == Command::WithOption) << 149 if (command->fType == Command::WithOption) >> 150 { 142 if (fVerbose) G4cout << "WithOption" << 151 if (fVerbose) G4cout << "WithOption" << G4endl; 143 152 144 if (i + 1 > firstArgc || argv[i + 1] == << 153 if(i+1 > firstArgc || argv[i+1]==0 || argv[i+1][0]=='-') 145 G4cerr << "An command line option is m << 154 { >> 155 G4cerr << "An command line option is missing for " >> 156 << marker << G4endl; 146 abort(); 157 abort(); 147 } 158 } 148 159 149 command->SetOption((const char*)strdup(a << 160 command->SetOption( (const char*) strdup(argv[i+1]) ); 150 argv[i + 1] = null; << 161 argv[i+1] = null; 151 i++; 162 i++; 152 } 163 } 153 else if (command->fType == Command::Option << 164 else if(command->fType == Command::OptionNotCompulsory) 154 if (fVerbose) G4cout << "OptionNotCompul << 165 { 155 << 166 if(fVerbose) 156 if (i + 1 < firstArgc) { << 167 G4cout <<"OptionNotCompulsory"<<G4endl; 157 G4String buffer = (const char*)strdup( << 158 168 159 if (buffer.empty() == false) { << 169 if(i+1 < firstArgc) 160 if (buffer.at(0) != '-' && buffer.at << 170 { 161 && buffer.at(0) != '|') << 171 G4String buffer = (const char*) strdup(argv[i+1]); >> 172 >> 173 if(buffer.empty() == false) >> 174 { >> 175 if(buffer.at(0) != '-' >> 176 && buffer.at(0) != '&' >> 177 && buffer.at(0) != '>' >> 178 && buffer.at(0) != '|') 162 { 179 { 163 if (fVerbose) { << 180 if(fVerbose) >> 181 { 164 G4cout << "facultative option is 182 G4cout << "facultative option is : " << buffer << G4endl; 165 } 183 } 166 184 167 command->SetOption((const char*)st << 185 command->SetOption( (const char*) strdup(argv[i+1]) ); 168 argv[i + 1] = null; << 186 argv[i+1] = null; 169 i++; 187 i++; 170 continue; 188 continue; 171 } 189 } 172 } 190 } 173 } 191 } 174 192 175 if (fVerbose) G4cout << "Option not set" << 193 if(fVerbose) >> 194 G4cout << "Option not set" << G4endl; 176 195 177 command->SetOption(""); 196 command->SetOption(""); 178 } 197 } 179 } 198 } 180 CorrectRemainingOptions(argc, argv); 199 CorrectRemainingOptions(argc, argv); 181 200 182 Command* commandLine(0); 201 Command* commandLine(0); 183 if ((commandLine = GetCommandIfActive("--hel << 202 if ((commandLine = GetCommandIfActive("--help")) || (commandLine = >> 203 GetCommandIfActive("-h"))) >> 204 { 184 G4cout << "Usage : " << argv[0] << " [OPTI 205 G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl; 185 PrintHelp(); 206 PrintHelp(); 186 return 1; 207 return 1; 187 } 208 } 188 209 189 return 0; 210 return 0; 190 } 211 } 191 212 192 //....oooOO0OOooo........oooOO0OOooo........oo 213 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 193 214 194 void CommandLineParser::PrintHelp() 215 void CommandLineParser::PrintHelp() 195 { 216 { 196 std::map<G4String, Command*>::iterator it; 217 std::map<G4String, Command*>::iterator it; 197 218 198 int maxFieldLength = fMaxMarkerLength + fMax 219 int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4; 199 220 200 G4cout << "Options: " << G4endl; 221 G4cout << "Options: " << G4endl; 201 222 202 for (it = fCommandMap.begin(); it != fComman << 223 for (it = fCommandMap.begin(); it != fCommandMap.end(); it++) >> 224 { 203 Command* command = it->second; 225 Command* command = it->second; 204 if (command) { << 226 if (command) >> 227 { 205 G4cout << setw(maxFieldLength) << left; 228 G4cout << setw(maxFieldLength) << left; 206 229 207 G4String toPrint = it->first; 230 G4String toPrint = it->first; 208 231 209 if (toPrint == "&") { << 232 if (toPrint == "&") >> 233 { 210 continue; 234 continue; 211 } 235 } 212 else if (toPrint == "-h") << 236 else if (toPrint == "-h") continue; 213 continue; << 237 else if (toPrint == "--help") 214 else if (toPrint == "--help") { << 238 { 215 toPrint += ", -h"; 239 toPrint += ", -h"; 216 } 240 } 217 241 218 if (command->GetDefaultOption() != "") { << 242 if (command->GetDefaultOption() != "") >> 243 { 219 toPrint += " \"" + command->GetDefault 244 toPrint += " \"" + command->GetDefaultOption() + "\""; 220 } 245 } 221 246 222 G4cout << toPrint; 247 G4cout << toPrint; 223 248 224 G4cout << command->GetDescription() << G 249 G4cout << command->GetDescription() << G4endl; 225 } 250 } 226 } 251 } 227 } 252 } 228 253 229 //....oooOO0OOooo........oooOO0OOooo........oo 254 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 230 255 231 void CommandLineParser::CorrectRemainingOption << 256 void CommandLineParser::CorrectRemainingOptions(int& argc, char **argv) 232 { 257 { 233 // remove handled arguments from argument ar 258 // remove handled arguments from argument array 234 int j = 0; 259 int j = 0; 235 for (int i = 0; i < argc; i++) { << 260 for (int i = 0; i < argc; i++) 236 if (strcmp(argv[i], "")) { << 261 { >> 262 if (strcmp(argv[i], "")) >> 263 { 237 argv[j] = argv[i]; 264 argv[j] = argv[i]; 238 j++; 265 j++; 239 } 266 } 240 } 267 } 241 argc = j; 268 argc = j; 242 } 269 } 243 270 244 //....oooOO0OOooo........oooOO0OOooo........oo 271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 245 272 246 void CommandLineParser::AddCommand(const G4Str << 273 void CommandLineParser::AddCommand(const G4String& marker, 247 const G4Str << 274 Command::Type type, >> 275 const G4String& description, >> 276 const G4String& defaultOption, 248 const G4Str 277 const G4String& optionName) 249 { 278 { 250 // G4cout << "Add command : "<< marker << G4 279 // G4cout << "Add command : "<< marker << G4endl; 251 << 280 252 Command* command = 0; 281 Command* command = 0; 253 switch (type) { << 282 switch(type) 254 case Command::WithoutOption: << 283 { 255 command = new Command(type, description) << 284 case Command::WithoutOption: 256 break; << 285 command = new Command(type, description); 257 << 286 break; 258 default: << 287 259 command = new CommandWithOption(type, de << 288 default: 260 if ((int)defaultOption.length() > fMaxOp << 289 command = new CommandWithOption(type, 261 fMaxOptionNameLength = defaultOption.l << 290 description, 262 break; << 291 defaultOption, >> 292 optionName); >> 293 if ((int) defaultOption.length() > fMaxOptionNameLength) >> 294 fMaxOptionNameLength = defaultOption.length(); >> 295 break; 263 } 296 } 264 297 265 if ((int)marker.length() > fMaxMarkerLength) << 298 if ((int) marker.length() > fMaxMarkerLength) fMaxMarkerLength = >> 299 marker.length(); 266 fCommandMap.insert(make_pair(marker, command 300 fCommandMap.insert(make_pair(marker, command)); 267 } 301 } 268 302 269 /* 303 /* 270 // Add one command but multiple markers 304 // Add one command but multiple markers 271 void Parser::AddCommand(vector<G4String> marke 305 void Parser::AddCommand(vector<G4String> markers, 272 CommandType type, 306 CommandType type, 273 const G4String& descri 307 const G4String& description, 274 const G4String& option 308 const G4String& optionName) 275 { 309 { 276 // G4cout << "Add command : "<< marker << G4 310 // G4cout << "Add command : "<< marker << G4endl; 277 Command* command = new Command(type, descrip 311 Command* command = new Command(type, description, optionName); 278 312 279 for (size_t i = 0; i < markers.size; i++) 313 for (size_t i = 0; i < markers.size; i++) 280 { 314 { 281 G4String marker = markers[i]; 315 G4String marker = markers[i]; 282 if ((int) marker.length() > fMaxMarkerLeng 316 if ((int) marker.length() > fMaxMarkerLength) 283 { 317 { 284 fMaxMarkerLength = marker.length(); 318 fMaxMarkerLength = marker.length(); 285 } 319 } 286 if ((int) optionName.length() > fMaxOption 320 if ((int) optionName.length() > fMaxOptionNameLength) 287 { 321 { 288 fMaxOptionNameLength = optionName.length 322 fMaxOptionNameLength = optionName.length(); 289 } 323 } 290 fCommandMap.insert(make_pair(marker, comma 324 fCommandMap.insert(make_pair(marker, command)); 291 } 325 } 292 } 326 } 293 */ 327 */ 294 328 295 //....oooOO0OOooo........oooOO0OOooo........oo 329 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 296 330 297 Command* CommandLineParser::FindCommand(const 331 Command* CommandLineParser::FindCommand(const G4String& marker) 298 { 332 { 299 std::map<G4String, Command*>::iterator it = 333 std::map<G4String, Command*>::iterator it = fCommandMap.find(marker); 300 if (it == fCommandMap.end()) { << 334 if (it == fCommandMap.end()) >> 335 { 301 // G4cerr << "command not found" << G4endl 336 // G4cerr << "command not found" << G4endl; 302 return 0; 337 return 0; 303 } 338 } 304 return it->second; 339 return it->second; 305 } 340 } 306 341 307 //....oooOO0OOooo........oooOO0OOooo........oo 342 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 308 343 309 Command* CommandLineParser::GetCommandIfActive << 344 Command* CommandLineParser::GetCommandIfActive(const G4String &marker) 310 { 345 { 311 Command* command = FindCommand(marker); 346 Command* command = FindCommand(marker); 312 if (command) { << 347 if (command) >> 348 { 313 // G4cout << "Command found : "<< marker < 349 // G4cout << "Command found : "<< marker << G4endl; 314 350 315 if (command->fActive) { << 351 if (command->fActive) >> 352 { 316 // G4cout << "Command Active" << G4endl; 353 // G4cout << "Command Active" << G4endl; 317 return command; 354 return command; 318 } 355 } 319 // else 356 // else 320 // G4cout <<"Command not active" << G4end 357 // G4cout <<"Command not active" << G4endl; 321 } 358 } 322 else { << 359 else >> 360 { 323 G4ExceptionDescription description; 361 G4ExceptionDescription description; 324 description << "You try to retrieve a comm << 362 description << "You try to retrieve a command that was not registered : " 325 G4Exception("CommandLineParser::GetCommand << 363 << marker << G4endl; 326 description, ""); << 364 G4Exception("CommandLineParser::GetCommandIfActive", >> 365 "COMMAND LINE NOT DEFINED", FatalException, description, ""); 327 // If you are using this class outside of 366 // If you are using this class outside of Geant4, use exit(-1) instead 328 // exit(-1); << 367 //exit(-1); 329 } 368 } 330 return 0; 369 return 0; 331 } 370 } 332 371 333 //....oooOO0OOooo........oooOO0OOooo........oo 372 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 334 373 335 bool CommandLineParser::CheckIfNotHandledOptio 374 bool CommandLineParser::CheckIfNotHandledOptionsExists(int& argc, char** argv) 336 { 375 { 337 if (argc > 0) { << 376 if (argc > 0) >> 377 { 338 G4bool kill = false; 378 G4bool kill = false; 339 for (G4int i = 1; i < argc; i++) { << 379 for (G4int i = 1; i < argc; i++) 340 if (strcmp(argv[i], "")) { << 380 { >> 381 if (strcmp(argv[i], "")) >> 382 { 341 kill = true; 383 kill = true; 342 G4cerr << "Unknown argument : " << arg 384 G4cerr << "Unknown argument : " << argv[i] << "\n"; 343 } 385 } 344 } 386 } 345 if (kill) { << 387 if (kill) 346 G4cerr << "The option " << argv[0] << " << 388 { >> 389 G4cerr << "The option " << argv[0] >> 390 << " is not handled this programme." << G4endl; 347 G4cout << "Usage : " << argv[0] << " [OP 391 G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl; 348 PrintHelp(); 392 PrintHelp(); 349 return true; // KILL APPLICATION << 393 return true; // KILL APPLICATION 350 } 394 } 351 } 395 } 352 return false; 396 return false; 353 } 397 } 354 398