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