41 #include "CommandLineParser.hh" 47 G4String Command::fNoOption =
"NoOption";
52 inline bool MATCH(
const char *
a,
const char *
b)
54 return strcmp(a, b) == 0;
59 CommandLineParser::CommandLineParser()
63 fOptionsWereSetup =
false;
65 fMaxOptionNameLength = 0;
66 AddCommand(
"--help", Command::WithoutOption,
"Print this help");
67 AddCommand(
"-h", Command::WithoutOption,
"Print this help");
68 AddCommand(
"&", Command::WithoutOption);
83 CommandLineParser::~CommandLineParser()
85 std::map<G4String, Command*>::iterator it = fCommandMap.begin();
86 for (; it != fCommandMap.end(); it++)
88 if (it->second)
delete it->second;
94 void CommandLineParser::DeleteInstance()
109 fDescription = description;
113 CommandWithOption::CommandWithOption(
Command::Type commandType,
117 Command(commandType, description)
119 fDefaultOption = defaultOption;
120 fOptionName = optionName;
130 static char null[1] = {
"" };
131 int firstArgc = argc;
133 for (
int i = 1; i < firstArgc; i++)
135 Command* command = FindCommand(argv[i]);
136 if (command == 0)
continue;
138 if (fVerbose)
G4cout <<
"Command : " << argv[i] <<
G4endl;
140 fOptionsWereSetup =
true;
145 if (strcmp(argv[i],
"-h") != 0 && strcmp(argv[i],
"--help") != 0)
150 if (command->
fType == Command::WithOption)
154 if(i+1 > firstArgc || argv[i+1]==0 || argv[i+1][0]==
'-')
156 G4cerr <<
"An command line option is missing for " 161 command->
SetOption( (
const char*) strdup(argv[i+1]) );
165 else if(command->
fType == Command::OptionNotCompulsory)
174 if(buffer.empty() ==
false)
176 if(buffer.at(0) !=
'-' 177 && buffer.at(0) !=
'&' 178 && buffer.at(0) !=
'>' 179 && buffer.at(0) !=
'|')
183 G4cout <<
"facultative option is : " << buffer <<
G4endl;
186 command->
SetOption( (
const char*) strdup(argv[i+1]) );
200 CorrectRemainingOptions(argc, argv);
203 if ((commandLine = GetCommandIfActive(
"--help")) || (commandLine =
204 GetCommandIfActive(
"-h")))
206 G4cout <<
"Usage : " << argv[0] <<
" [OPTIONS]" <<
G4endl;
216 void CommandLineParser::PrintHelp()
218 std::map<G4String, Command*>::iterator it;
220 int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4;
224 for (it = fCommandMap.begin(); it != fCommandMap.end(); it++)
237 else if (toPrint ==
"-h")
continue;
238 else if (toPrint ==
"--help")
257 void CommandLineParser::CorrectRemainingOptions(
int& argc,
char **argv)
261 for (
int i = 0; i < argc; i++)
263 if (strcmp(argv[i],
""))
274 void CommandLineParser::AddCommand(
const G4String& marker,
285 case Command::WithoutOption:
286 command =
new Command(type, description);
294 if ((
int) defaultOption.length() > fMaxOptionNameLength)
295 fMaxOptionNameLength = defaultOption.length();
299 if ((
int) marker.length() > fMaxMarkerLength) fMaxMarkerLength =
301 fCommandMap.insert(make_pair(marker, command));
334 std::map<G4String, Command*>::iterator it = fCommandMap.find(marker);
335 if (it == fCommandMap.end())
347 Command* command = FindCommand(marker);
363 description <<
"You try to retrieve a command that was not registered : " 365 G4Exception(
"CommandLineParser::GetCommandIfActive",
375 bool CommandLineParser::CheckIfNotHandledOptionsExists(
int& argc,
char** argv)
380 for (
G4int i = 1; i < argc; i++)
382 if (strcmp(argv[i],
""))
385 G4cerr <<
"Unknown argument : " << argv[i] <<
"\n";
390 G4cerr <<
"The option " << argv[0]
391 <<
" is not handled this programme." <<
G4endl;
392 G4cout <<
"Usage : " << argv[0] <<
" [OPTIONS]" <<
G4endl;
void Parse(int &argc, char **argv)
std::ostringstream G4ExceptionDescription
virtual const G4String & GetDefaultOption()
const G4String & GetDescription()
virtual void SetOption(const G4String &)
bool MATCH(const char *a, const char *b)
G4GLOB_DLL std::ostream G4cout
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
G4GLOB_DLL std::ostream G4cerr