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 49 //....oooOO0OOooo........oooOO0OOooo........oo 48 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 50 49 51 inline bool MATCH(const char* a, const char* b << 50 inline bool MATCH(const char *a, const char *b) 52 { 51 { 53 return strcmp(a, b) == 0; 52 return strcmp(a, b) == 0; 54 } 53 } 55 54 56 //....oooOO0OOooo........oooOO0OOooo........oo 55 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 57 56 58 CommandLineParser::CommandLineParser() 57 CommandLineParser::CommandLineParser() 59 { 58 { 60 // G4cout << "############ NEW PARSE ####### 59 // G4cout << "############ NEW PARSE ##########" << G4endl; 61 fpInstance = this; 60 fpInstance = this; 62 fOptionsWereSetup = false; 61 fOptionsWereSetup = false; 63 fMaxMarkerLength = 0; 62 fMaxMarkerLength = 0; 64 fMaxOptionNameLength = 0; 63 fMaxOptionNameLength = 0; 65 AddCommand("--help", Command::WithoutOption, 64 AddCommand("--help", Command::WithoutOption, "Print this help"); 66 AddCommand("-h", Command::WithoutOption, "Pr 65 AddCommand("-h", Command::WithoutOption, "Print this help"); 67 AddCommand("&", Command::WithoutOption); 66 AddCommand("&", Command::WithoutOption); 68 67 69 fVerbose = 0; 68 fVerbose = 0; 70 } 69 } 71 70 72 //....oooOO0OOooo........oooOO0OOooo........oo 71 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 73 72 74 CommandLineParser* CommandLineParser::GetParse 73 CommandLineParser* CommandLineParser::GetParser() 75 { 74 { 76 if (!fpInstance) new CommandLineParser; 75 if (!fpInstance) new CommandLineParser; 77 return fpInstance; 76 return fpInstance; 78 } 77 } 79 78 80 //....oooOO0OOooo........oooOO0OOooo........oo 79 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 81 80 82 CommandLineParser::~CommandLineParser() 81 CommandLineParser::~CommandLineParser() 83 { 82 { 84 std::map<G4String, Command*>::iterator it = 83 std::map<G4String, Command*>::iterator it = fCommandMap.begin(); 85 for (; it != fCommandMap.end(); it++) { << 84 for (; it != fCommandMap.end(); it++) >> 85 { 86 if (it->second) delete it->second; 86 if (it->second) delete it->second; 87 } 87 } 88 } 88 } 89 89 90 //....oooOO0OOooo........oooOO0OOooo........oo 90 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 91 91 92 void CommandLineParser::DeleteInstance() 92 void CommandLineParser::DeleteInstance() 93 { 93 { 94 if (fpInstance) { << 94 if (fpInstance) >> 95 { 95 delete fpInstance; 96 delete fpInstance; 96 fpInstance = 0; 97 fpInstance = 0; 97 } 98 } 98 } 99 } 99 100 100 //....oooOO0OOooo........oooOO0OOooo........oo 101 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 101 102 102 Command::Command(Command::Type commandType, co << 103 Command::Command(Command::Type commandType, >> 104 const G4String& description) 103 { 105 { 104 fType = commandType; 106 fType = commandType; 105 fDescription = description; 107 fDescription = description; 106 fActive = false; 108 fActive = false; 107 } 109 } 108 110 109 //....oooOO0OOooo........oooOO0OOooo........oo 111 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 110 112 111 CommandWithOption::CommandWithOption(Command:: << 113 CommandWithOption::CommandWithOption(Command::Type commandType, 112 const G4S << 114 const G4String& description, 113 : Command(commandType, description) << 115 const G4String& defaultOption, >> 116 const G4String& optionName) : >> 117 Command(commandType, description) 114 { 118 { 115 fDefaultOption = defaultOption; 119 fDefaultOption = defaultOption; 116 fOptionName = optionName; 120 fOptionName = optionName; 117 fOption = ""; 121 fOption = ""; 118 } 122 } 119 123 120 //....oooOO0OOooo........oooOO0OOooo........oo 124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 121 125 122 int CommandLineParser::Parse(int& argc, char** << 126 int CommandLineParser::Parse(int& argc, char **argv) 123 { 127 { 124 // G4cout << "Parse " << G4endl; 128 // G4cout << "Parse " << G4endl; 125 static char null[1] = {""}; << 129 static char null[1] = { "" }; 126 int firstArgc = argc; 130 int firstArgc = argc; 127 131 128 for (int i = 1; i < firstArgc; i++) { << 132 for (int i = 1; i < firstArgc; i++) >> 133 { 129 Command* command = FindCommand(argv[i]); 134 Command* command = FindCommand(argv[i]); 130 if (command == 0) continue; 135 if (command == 0) continue; 131 136 132 if (fVerbose) G4cout << "Command : " << ar 137 if (fVerbose) G4cout << "Command : " << argv[i] << G4endl; 133 138 134 fOptionsWereSetup = true; 139 fOptionsWereSetup = true; 135 command->fActive = true; 140 command->fActive = true; 136 141 137 G4String marker(argv[i]); 142 G4String marker(argv[i]); 138 143 139 if (strcmp(argv[i], "-h") != 0 && strcmp(a << 144 if (strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "--help") != 0) >> 145 { 140 argv[i] = null; 146 argv[i] = null; 141 } 147 } 142 148 143 if (command->fType == Command::WithOption) << 149 if (command->fType == Command::WithOption) >> 150 { 144 if (fVerbose) G4cout << "WithOption" << 151 if (fVerbose) G4cout << "WithOption" << G4endl; 145 152 146 if (i + 1 > firstArgc || argv[i + 1] == << 153 if(i+1 > firstArgc || argv[i+1]==0 || argv[i+1][0]=='-') 147 G4cerr << "An command line option is m << 154 { >> 155 G4cerr << "An command line option is missing for " >> 156 << marker << G4endl; 148 abort(); 157 abort(); 149 } 158 } 150 159 151 command->SetOption((const char*)strdup(a << 160 command->SetOption( (const char*) strdup(argv[i+1]) ); 152 argv[i + 1] = null; << 161 argv[i+1] = null; 153 i++; 162 i++; 154 } 163 } 155 else if (command->fType == Command::Option << 164 else if(command->fType == Command::OptionNotCompulsory) 156 if (fVerbose) G4cout << "OptionNotCompul << 165 { 157 << 166 if(fVerbose) 158 if (i + 1 < firstArgc) { << 167 G4cout <<"OptionNotCompulsory"<<G4endl; 159 G4String buffer = (const char*)strdup( << 160 168 161 if (buffer.empty() == false) { << 169 if(i+1 < firstArgc) 162 if (buffer.at(0) != '-' && buffer.at << 170 { 163 && 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) != '|') 164 { 179 { 165 if (fVerbose) { << 180 if(fVerbose) >> 181 { 166 G4cout << "facultative option is 182 G4cout << "facultative option is : " << buffer << G4endl; 167 } 183 } 168 184 169 command->SetOption((const char*)st << 185 command->SetOption( (const char*) strdup(argv[i+1]) ); 170 argv[i + 1] = null; << 186 argv[i+1] = null; 171 i++; 187 i++; 172 continue; 188 continue; 173 } 189 } 174 } 190 } 175 } 191 } 176 192 177 if (fVerbose) G4cout << "Option not set" << 193 if(fVerbose) >> 194 G4cout << "Option not set" << G4endl; 178 195 179 command->SetOption(""); 196 command->SetOption(""); 180 } 197 } 181 } 198 } 182 CorrectRemainingOptions(argc, argv); 199 CorrectRemainingOptions(argc, argv); 183 200 184 Command* commandLine(0); 201 Command* commandLine(0); 185 if ((commandLine = GetCommandIfActive("--hel << 202 if ((commandLine = GetCommandIfActive("--help")) || (commandLine = >> 203 GetCommandIfActive("-h"))) >> 204 { 186 G4cout << "Usage : " << argv[0] << " [OPTI 205 G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl; 187 PrintHelp(); 206 PrintHelp(); 188 return 1; 207 return 1; 189 } 208 } 190 209 191 return 0; 210 return 0; 192 } 211 } 193 212 194 //....oooOO0OOooo........oooOO0OOooo........oo 213 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 195 214 196 void CommandLineParser::PrintHelp() 215 void CommandLineParser::PrintHelp() 197 { 216 { 198 std::map<G4String, Command*>::iterator it; 217 std::map<G4String, Command*>::iterator it; 199 218 200 int maxFieldLength = fMaxMarkerLength + fMax 219 int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4; 201 220 202 G4cout << "Options: " << G4endl; 221 G4cout << "Options: " << G4endl; 203 222 204 for (it = fCommandMap.begin(); it != fComman << 223 for (it = fCommandMap.begin(); it != fCommandMap.end(); it++) >> 224 { 205 Command* command = it->second; 225 Command* command = it->second; 206 if (command) { << 226 if (command) >> 227 { 207 G4cout << setw(maxFieldLength) << left; 228 G4cout << setw(maxFieldLength) << left; 208 229 209 G4String toPrint = it->first; 230 G4String toPrint = it->first; 210 231 211 if (toPrint == "&") { << 232 if (toPrint == "&") >> 233 { 212 continue; 234 continue; 213 } 235 } 214 else if (toPrint == "-h") << 236 else if (toPrint == "-h") continue; 215 continue; << 237 else if (toPrint == "--help") 216 else if (toPrint == "--help") { << 238 { 217 toPrint += ", -h"; 239 toPrint += ", -h"; 218 } 240 } 219 241 220 if (command->GetDefaultOption() != "") { << 242 if (command->GetDefaultOption() != "") >> 243 { 221 toPrint += " \"" + command->GetDefault 244 toPrint += " \"" + command->GetDefaultOption() + "\""; 222 } 245 } 223 246 224 G4cout << toPrint; 247 G4cout << toPrint; 225 248 226 G4cout << command->GetDescription() << G 249 G4cout << command->GetDescription() << G4endl; 227 } 250 } 228 } 251 } 229 } 252 } 230 253 231 //....oooOO0OOooo........oooOO0OOooo........oo 254 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 232 255 233 void CommandLineParser::CorrectRemainingOption << 256 void CommandLineParser::CorrectRemainingOptions(int& argc, char **argv) 234 { 257 { 235 // remove handled arguments from argument ar 258 // remove handled arguments from argument array 236 int j = 0; 259 int j = 0; 237 for (int i = 0; i < argc; i++) { << 260 for (int i = 0; i < argc; i++) 238 if (strcmp(argv[i], "")) { << 261 { >> 262 if (strcmp(argv[i], "")) >> 263 { 239 argv[j] = argv[i]; 264 argv[j] = argv[i]; 240 j++; 265 j++; 241 } 266 } 242 } 267 } 243 argc = j; 268 argc = j; 244 } 269 } 245 270 246 //....oooOO0OOooo........oooOO0OOooo........oo 271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 247 272 248 void CommandLineParser::AddCommand(const G4Str << 273 void CommandLineParser::AddCommand(const G4String& marker, 249 const G4Str << 274 Command::Type type, >> 275 const G4String& description, >> 276 const G4String& defaultOption, 250 const G4Str 277 const G4String& optionName) 251 { 278 { 252 // G4cout << "Add command : "<< marker << G4 279 // G4cout << "Add command : "<< marker << G4endl; 253 << 280 254 Command* command = 0; 281 Command* command = 0; 255 switch (type) { << 282 switch(type) 256 case Command::WithoutOption: << 283 { 257 command = new Command(type, description) << 284 case Command::WithoutOption: 258 break; << 285 command = new Command(type, description); 259 << 286 break; 260 default: << 287 261 command = new CommandWithOption(type, de << 288 default: 262 if ((int)defaultOption.length() > fMaxOp << 289 command = new CommandWithOption(type, 263 fMaxOptionNameLength = defaultOption.l << 290 description, 264 break; << 291 defaultOption, >> 292 optionName); >> 293 if ((int) defaultOption.length() > fMaxOptionNameLength) >> 294 fMaxOptionNameLength = defaultOption.length(); >> 295 break; 265 } 296 } 266 297 267 if ((int)marker.length() > fMaxMarkerLength) << 298 if ((int) marker.length() > fMaxMarkerLength) fMaxMarkerLength = >> 299 marker.length(); 268 fCommandMap.insert(make_pair(marker, command 300 fCommandMap.insert(make_pair(marker, command)); 269 } 301 } 270 302 271 //....oooOO0OOooo........oooOO0OOooo........oo 303 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 272 304 273 /* 305 /* 274 // Add one command but multiple markers 306 // Add one command but multiple markers 275 void Parser::AddCommand(vector<G4String> marke 307 void Parser::AddCommand(vector<G4String> markers, 276 CommandType type, 308 CommandType type, 277 const G4String& descri 309 const G4String& description, 278 const G4String& option 310 const G4String& optionName) 279 { 311 { 280 // G4cout << "Add command : "<< marker << G4 312 // G4cout << "Add command : "<< marker << G4endl; 281 Command* command = new Command(type, descrip 313 Command* command = new Command(type, description, optionName); 282 314 283 for (size_t i = 0; i < markers.size; i++) 315 for (size_t i = 0; i < markers.size; i++) 284 { 316 { 285 G4String marker = markers[i]; 317 G4String marker = markers[i]; 286 if ((int) marker.length() > fMaxMarkerLeng 318 if ((int) marker.length() > fMaxMarkerLength) 287 { 319 { 288 fMaxMarkerLength = marker.length(); 320 fMaxMarkerLength = marker.length(); 289 } 321 } 290 if ((int) optionName.length() > fMaxOption 322 if ((int) optionName.length() > fMaxOptionNameLength) 291 { 323 { 292 fMaxOptionNameLength = optionName.length 324 fMaxOptionNameLength = optionName.length(); 293 } 325 } 294 fCommandMap.insert(make_pair(marker, comma 326 fCommandMap.insert(make_pair(marker, command)); 295 } 327 } 296 } 328 } 297 */ 329 */ 298 330 299 //....oooOO0OOooo........oooOO0OOooo........oo 331 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 300 332 301 Command* CommandLineParser::FindCommand(const 333 Command* CommandLineParser::FindCommand(const G4String& marker) 302 { 334 { 303 std::map<G4String, Command*>::iterator it = 335 std::map<G4String, Command*>::iterator it = fCommandMap.find(marker); 304 if (it == fCommandMap.end()) { << 336 if (it == fCommandMap.end()) >> 337 { 305 // G4cerr << "command not found" << G4endl 338 // G4cerr << "command not found" << G4endl; 306 return 0; 339 return 0; 307 } 340 } 308 return it->second; 341 return it->second; 309 } 342 } 310 343 311 //....oooOO0OOooo........oooOO0OOooo........oo 344 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 312 345 313 Command* CommandLineParser::GetCommandIfActive << 346 Command* CommandLineParser::GetCommandIfActive(const G4String &marker) 314 { 347 { 315 Command* command = FindCommand(marker); 348 Command* command = FindCommand(marker); 316 if (command) { << 349 if (command) >> 350 { 317 // G4cout << "Command found : "<< marker < 351 // G4cout << "Command found : "<< marker << G4endl; 318 352 319 if (command->fActive) { << 353 if (command->fActive) >> 354 { 320 // G4cout << "Command Active" << G4endl; 355 // G4cout << "Command Active" << G4endl; 321 return command; 356 return command; 322 } 357 } 323 // else 358 // else 324 // G4cout <<"Command not active" << G4end 359 // G4cout <<"Command not active" << G4endl; 325 } 360 } 326 else { << 361 else >> 362 { 327 G4ExceptionDescription description; 363 G4ExceptionDescription description; 328 description << "You try to retrieve a comm << 364 description << "You try to retrieve a command that was not registered : " 329 G4Exception("CommandLineParser::GetCommand << 365 << marker << G4endl; 330 description, ""); << 366 G4Exception("CommandLineParser::GetCommandIfActive", >> 367 "COMMAND LINE NOT DEFINED", FatalException, description, ""); 331 // If you are using this class outside of 368 // If you are using this class outside of Geant4, use exit(-1) instead 332 // exit(-1); << 369 //exit(-1); 333 } 370 } 334 return 0; 371 return 0; 335 } 372 } 336 373 337 //....oooOO0OOooo........oooOO0OOooo........oo 374 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 338 375 339 bool CommandLineParser::CheckIfNotHandledOptio 376 bool CommandLineParser::CheckIfNotHandledOptionsExists(int& argc, char** argv) 340 { 377 { 341 if (argc > 0) { << 378 if (argc > 0) >> 379 { 342 G4bool kill = false; 380 G4bool kill = false; 343 for (G4int i = 1; i < argc; i++) { << 381 for (G4int i = 1; i < argc; i++) 344 if (strcmp(argv[i], "")) { << 382 { >> 383 if (strcmp(argv[i], "")) >> 384 { 345 kill = true; 385 kill = true; 346 G4cerr << "Unknown argument : " << arg 386 G4cerr << "Unknown argument : " << argv[i] << "\n"; 347 } 387 } 348 } 388 } 349 if (kill) { << 389 if (kill) 350 G4cerr << "The option " << argv[0] << " << 390 { >> 391 G4cerr << "The option " << argv[0] >> 392 << " is not handled this programme." << G4endl; 351 G4cout << "Usage : " << argv[0] << " [OP 393 G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl; 352 PrintHelp(); 394 PrintHelp(); 353 return true; // KILL APPLICATION << 395 return true; // KILL APPLICATION 354 } 396 } 355 } 397 } 356 return false; 398 return false; 357 } 399 } 358 400