40 #include "CommandLineParser.hh"    46 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;
   129   static char null[1] = { 
"" };
   130   int firstArgc = argc;
   132   for (
int i = 1; i < firstArgc; i++)
   134     Command* command = FindCommand(argv[i]);
   135     if (command == 0) 
continue;
   137     if (fVerbose) 
G4cout << 
"Command : " << argv[i] << 
G4endl;
   139     fOptionsWereSetup = 
true;
   144     if (strcmp(argv[i], 
"-h") != 0 && strcmp(argv[i], 
"--help") != 0)
   149     if (command->
fType == Command::WithOption)
   153       if(i+1 > firstArgc || argv[i+1]==0 || argv[i+1][0]==
'-')
   155         G4cerr << 
"An command line option is missing for "   160       command->
SetOption( (
const char*) strdup(argv[i+1]) );
   164     else if(command->
fType == Command::OptionNotCompulsory)
   173         if(buffer.empty() == 
false)
   175           if(buffer.at(0) != 
'-'   176               && buffer.at(0) != 
'&'   177                   && buffer.at(0) != 
'>'   178                       && buffer.at(0) != 
'|')
   182               G4cout << 
"facultative option is : " << buffer << 
G4endl;
   185             command->
SetOption( (
const char*) strdup(argv[i+1]) );
   199   CorrectRemainingOptions(argc, argv);
   202   if ((commandLine = GetCommandIfActive(
"--help")) || (commandLine =
   203       GetCommandIfActive(
"-h")))
   205     G4cout << 
"Usage : " << argv[0] << 
" [OPTIONS]" << 
G4endl;
   215 void CommandLineParser::PrintHelp()
   217   std::map<G4String, Command*>::iterator it;
   219   int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4;
   223   for (it = fCommandMap.begin(); it != fCommandMap.end(); it++)
   236       else if (toPrint == 
"-h") 
continue;
   237       else if (toPrint == 
"--help")
   256 void CommandLineParser::CorrectRemainingOptions(
int& argc, 
char **argv)
   260   for (
int i = 0; i < argc; i++)
   262     if (strcmp(argv[i], 
""))
   273 void CommandLineParser::AddCommand(
const G4String& marker,
   284     case Command::WithoutOption:
   285       command = 
new Command(type, description);
   293       if ((
int) defaultOption.length() > fMaxOptionNameLength)
   294         fMaxOptionNameLength = defaultOption.length();
   298   if ((
int) marker.length() > fMaxMarkerLength) fMaxMarkerLength =
   300   fCommandMap.insert(make_pair(marker, command));
   333   std::map<G4String, Command*>::iterator it = fCommandMap.find(marker);
   334   if (it == fCommandMap.end())
   346   Command* command = FindCommand(marker);
   362     description << 
"You try to retrieve a command that was not registered : "   364     G4Exception(
"CommandLineParser::GetCommandIfActive",
   374 bool CommandLineParser::CheckIfNotHandledOptionsExists(
int& argc, 
char** argv)
   379     for (
G4int i = 1; i < argc; i++)
   381       if (strcmp(argv[i], 
""))
   384         G4cerr << 
"Unknown argument : " << argv[i] << 
"\n";
   389       G4cerr << 
"The option " << argv[0]
   390                                       << 
" is not handled this programme."   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