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