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