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