41 #include "CommandLineParser.hh" 47 G4String Command::fNoOption =
"NoOption";
51 inline bool MATCH(
const char *
a,
const char *
b)
53 return strcmp(a, b) == 0;
58 CommandLineParser::CommandLineParser()
62 fOptionsWereSetup =
false;
64 fMaxOptionNameLength = 0;
65 AddCommand(
"--help", Command::WithoutOption,
"Print this help");
66 AddCommand(
"-h", Command::WithoutOption,
"Print this help");
67 AddCommand(
"&", Command::WithoutOption);
82 CommandLineParser::~CommandLineParser()
84 std::map<G4String, Command*>::iterator it = fCommandMap.begin();
85 for (; it != fCommandMap.end(); it++)
87 if (it->second)
delete it->second;
93 void CommandLineParser::DeleteInstance()
108 fDescription = description;
112 CommandWithOption::CommandWithOption(
Command::Type commandType,
116 Command(commandType, description)
118 fDefaultOption = defaultOption;
119 fOptionName = optionName;
128 static char null[1] = {
"" };
129 int firstArgc = argc;
131 for (
int i = 1; i < firstArgc; i++)
133 Command* command = FindCommand(argv[i]);
134 if (command == 0)
continue;
136 if (fVerbose)
G4cout <<
"Command : " << argv[i] <<
G4endl;
138 fOptionsWereSetup =
true;
143 if (strcmp(argv[i],
"-h") != 0 && strcmp(argv[i],
"--help") != 0)
148 if (command->
fType == Command::WithOption)
152 if(i+1 > firstArgc || argv[i+1]==0 || argv[i+1][0]==
'-')
154 G4cerr <<
"An command line option is missing for " 159 command->
SetOption( (
const char*) strdup(argv[i+1]) );
163 else if(command->
fType == Command::OptionNotCompulsory)
172 if(buffer.empty() ==
false)
174 if(buffer.at(0) !=
'-' 175 && buffer.at(0) !=
'&' 176 && buffer.at(0) !=
'>' 177 && buffer.at(0) !=
'|')
181 G4cout <<
"facultative option is : " << buffer <<
G4endl;
184 command->
SetOption( (
const char*) strdup(argv[i+1]) );
198 CorrectRemainingOptions(argc, argv);
201 if ((commandLine = GetCommandIfActive(
"--help")) || (commandLine =
202 GetCommandIfActive(
"-h")))
204 G4cout <<
"Usage : " << argv[0] <<
" [OPTIONS]" <<
G4endl;
214 void CommandLineParser::PrintHelp()
216 std::map<G4String, Command*>::iterator it;
218 int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4;
222 for (it = fCommandMap.begin(); it != fCommandMap.end(); it++)
235 else if (toPrint ==
"-h")
continue;
236 else if (toPrint ==
"--help")
255 void CommandLineParser::CorrectRemainingOptions(
int& argc,
char **argv)
259 for (
int i = 0; i < argc; i++)
261 if (strcmp(argv[i],
""))
272 void CommandLineParser::AddCommand(
const G4String& marker,
283 case Command::WithoutOption:
284 command =
new Command(type, description);
292 if ((
int) defaultOption.length() > fMaxOptionNameLength)
293 fMaxOptionNameLength = defaultOption.length();
297 if ((
int) marker.length() > fMaxMarkerLength) fMaxMarkerLength =
299 fCommandMap.insert(make_pair(marker, command));
332 std::map<G4String, Command*>::iterator it = fCommandMap.find(marker);
333 if (it == fCommandMap.end())
345 Command* command = FindCommand(marker);
361 description <<
"You try to retrieve a command that was not registered : " 363 G4Exception(
"CommandLineParser::GetCommandIfActive",
373 bool CommandLineParser::CheckIfNotHandledOptionsExists(
int& argc,
char** argv)
378 for (
G4int i = 1; i < argc; i++)
380 if (strcmp(argv[i],
""))
383 G4cerr <<
"Unknown argument : " << argv[i] <<
"\n";
388 G4cerr <<
"The option " << argv[0]
389 <<
" is not handled this programme." <<
G4endl;
390 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 &)
G4GLOB_DLL std::ostream G4cout
bool MATCH(const char *a, const char *b)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
G4GLOB_DLL std::ostream G4cerr