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