File indexing completed on 2025-02-23 09:21:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 #include "CommandLineParser.hh"
0039
0040 #include <iomanip>
0041
0042 using namespace std;
0043 using namespace G4DNAPARSER;
0044
0045 CommandLineParser* CommandLineParser::fpInstance(0);
0046 G4String Command::fNoOption = "NoOption";
0047
0048
0049
0050 inline bool MATCH(const char* a, const char* b)
0051 {
0052 return strcmp(a, b) == 0;
0053 }
0054
0055
0056
0057 CommandLineParser::CommandLineParser()
0058 {
0059
0060 fpInstance = this;
0061 fOptionsWereSetup = false;
0062 fMaxMarkerLength = 0;
0063 fMaxOptionNameLength = 0;
0064 AddCommand("--help", Command::WithoutOption, "Print this help");
0065 AddCommand("-h", Command::WithoutOption, "Print this help");
0066 AddCommand("&", Command::WithoutOption);
0067
0068 fVerbose = 0;
0069 }
0070
0071
0072
0073 CommandLineParser* CommandLineParser::GetParser()
0074 {
0075 if (!fpInstance) new CommandLineParser;
0076 return fpInstance;
0077 }
0078
0079
0080
0081 CommandLineParser::~CommandLineParser()
0082 {
0083 std::map<G4String, Command*>::iterator it = fCommandMap.begin();
0084 for (; it != fCommandMap.end(); it++) {
0085 if (it->second) delete it->second;
0086 }
0087 }
0088
0089
0090
0091 void CommandLineParser::DeleteInstance()
0092 {
0093 if (fpInstance) {
0094 delete fpInstance;
0095 fpInstance = 0;
0096 }
0097 }
0098
0099
0100
0101 Command::Command(Command::Type commandType, const G4String& description)
0102 {
0103 fType = commandType;
0104 fDescription = description;
0105 fActive = false;
0106 }
0107
0108 CommandWithOption::CommandWithOption(Command::Type commandType, const G4String& description,
0109 const G4String& defaultOption, const G4String& optionName)
0110 : Command(commandType, description)
0111 {
0112 fDefaultOption = defaultOption;
0113 fOptionName = optionName;
0114 fOption = "";
0115 }
0116
0117
0118
0119 int CommandLineParser::Parse(int& argc, char** argv)
0120 {
0121
0122 static char null[1] = {""};
0123 int firstArgc = argc;
0124
0125 for (int i = 1; i < firstArgc; i++) {
0126 Command* command = FindCommand(argv[i]);
0127 if (command == 0) continue;
0128
0129 if (fVerbose) G4cout << "Command : " << argv[i] << G4endl;
0130
0131 fOptionsWereSetup = true;
0132 command->fActive = true;
0133
0134 G4String marker(argv[i]);
0135
0136 if (strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "--help") != 0) {
0137 argv[i] = null;
0138 }
0139
0140 if (command->fType == Command::WithOption) {
0141 if (fVerbose) G4cout << "WithOption" << G4endl;
0142
0143 if (i + 1 > firstArgc || argv[i + 1] == 0 || argv[i + 1][0] == '-') {
0144 G4cerr << "An command line option is missing for " << marker << G4endl;
0145 abort();
0146 }
0147
0148 command->SetOption((const char*)strdup(argv[i + 1]));
0149 argv[i + 1] = null;
0150 i++;
0151 }
0152 else if (command->fType == Command::OptionNotCompulsory) {
0153 if (fVerbose) G4cout << "OptionNotCompulsory" << G4endl;
0154
0155 if (i + 1 < firstArgc) {
0156 G4String buffer = (const char*)strdup(argv[i + 1]);
0157
0158 if (buffer.empty() == false) {
0159 if (buffer.at(0) != '-' && buffer.at(0) != '&' && buffer.at(0) != '>'
0160 && buffer.at(0) != '|')
0161 {
0162 if (fVerbose) {
0163 G4cout << "facultative option is : " << buffer << G4endl;
0164 }
0165
0166 command->SetOption((const char*)strdup(argv[i + 1]));
0167 argv[i + 1] = null;
0168 i++;
0169 continue;
0170 }
0171 }
0172 }
0173
0174 if (fVerbose) G4cout << "Option not set" << G4endl;
0175
0176 command->SetOption("");
0177 }
0178 }
0179 CorrectRemainingOptions(argc, argv);
0180
0181 Command* commandLine(0);
0182 if ((commandLine = GetCommandIfActive("--help")) || (commandLine = GetCommandIfActive("-h"))) {
0183 G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
0184 PrintHelp();
0185 return 1;
0186 }
0187
0188 return 0;
0189 }
0190
0191
0192
0193 void CommandLineParser::PrintHelp()
0194 {
0195 std::map<G4String, Command*>::iterator it;
0196
0197 int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4;
0198
0199 G4cout << "Options: " << G4endl;
0200
0201 for (it = fCommandMap.begin(); it != fCommandMap.end(); it++) {
0202 Command* command = it->second;
0203 if (command) {
0204 G4cout << setw(maxFieldLength) << left;
0205
0206 G4String toPrint = it->first;
0207
0208 if (toPrint == "&") {
0209 continue;
0210 }
0211 else if (toPrint == "-h")
0212 continue;
0213 else if (toPrint == "--help") {
0214 toPrint += ", -h";
0215 }
0216
0217 if (command->GetDefaultOption() != "") {
0218 toPrint += " \"" + command->GetDefaultOption() + "\"";
0219 }
0220
0221 G4cout << toPrint;
0222
0223 G4cout << command->GetDescription() << G4endl;
0224 }
0225 }
0226 }
0227
0228
0229
0230 void CommandLineParser::CorrectRemainingOptions(int& argc, char** argv)
0231 {
0232
0233 int j = 0;
0234 for (int i = 0; i < argc; i++) {
0235 if (strcmp(argv[i], "")) {
0236 argv[j] = argv[i];
0237 j++;
0238 }
0239 }
0240 argc = j;
0241 }
0242
0243
0244
0245 void CommandLineParser::AddCommand(const G4String& marker, Command::Type type,
0246 const G4String& description, const G4String& defaultOption,
0247 const G4String& optionName)
0248 {
0249
0250
0251 Command* command = 0;
0252 switch (type) {
0253 case Command::WithoutOption:
0254 command = new Command(type, description);
0255 break;
0256
0257 default:
0258 command = new CommandWithOption(type, description, defaultOption, optionName);
0259 if ((int)defaultOption.length() > fMaxOptionNameLength)
0260 fMaxOptionNameLength = defaultOption.length();
0261 break;
0262 }
0263
0264 if ((int)marker.length() > fMaxMarkerLength) fMaxMarkerLength = marker.length();
0265 fCommandMap.insert(make_pair(marker, command));
0266 }
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 Command* CommandLineParser::FindCommand(const G4String& marker)
0297 {
0298 std::map<G4String, Command*>::iterator it = fCommandMap.find(marker);
0299 if (it == fCommandMap.end()) {
0300
0301 return 0;
0302 }
0303 return it->second;
0304 }
0305
0306
0307
0308 Command* CommandLineParser::GetCommandIfActive(const G4String& marker)
0309 {
0310 Command* command = FindCommand(marker);
0311 if (command) {
0312
0313
0314 if (command->fActive) {
0315
0316 return command;
0317 }
0318
0319
0320 }
0321 else {
0322 G4ExceptionDescription description;
0323 description << "You try to retrieve a command that was not registered : " << marker << G4endl;
0324 G4Exception("CommandLineParser::GetCommandIfActive", "COMMAND LINE NOT DEFINED", FatalException,
0325 description, "");
0326
0327
0328 }
0329 return 0;
0330 }
0331
0332
0333
0334 bool CommandLineParser::CheckIfNotHandledOptionsExists(int& argc, char** argv)
0335 {
0336 if (argc > 0) {
0337 G4bool kill = false;
0338 for (G4int i = 1; i < argc; i++) {
0339 if (strcmp(argv[i], "")) {
0340 kill = true;
0341 G4cerr << "Unknown argument : " << argv[i] << "\n";
0342 }
0343 }
0344 if (kill) {
0345 G4cerr << "The option " << argv[0] << " is not handled this programme." << G4endl;
0346 G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
0347 PrintHelp();
0348 return true;
0349 }
0350 }
0351 return false;
0352 }