Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4DNAPARSER::CommandLineParser Class Reference

#include <CommandLineParser.hh>

Public Member Functions

 CommandLineParser ()
 
 ~CommandLineParser ()
 
int Parse (int &argc, char **argv)
 
void PrintHelp ()
 
bool CheckIfNotHandledOptionsExists (int &argc, char **argv)
 
void CorrectRemainingOptions (int &argc, char **argv)
 
void AddCommand (const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
 
CommandFindCommand (const G4String &marker)
 
CommandGetCommandIfActive (const G4String &marker)
 
G4bool WereOptionsSetup ()
 
 CommandLineParser ()
 
 ~CommandLineParser ()
 
int Parse (int &argc, char **argv)
 
void PrintHelp ()
 
bool CheckIfNotHandledOptionsExists (int &argc, char **argv)
 
void CorrectRemainingOptions (int &argc, char **argv)
 
void AddCommand (const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
 
CommandFindCommand (const G4String &marker)
 
CommandGetCommandIfActive (const G4String &marker)
 
G4bool WereOptionsSetup ()
 
 CommandLineParser ()
 
 ~CommandLineParser ()
 
int Parse (int &argc, char **argv)
 
void PrintHelp ()
 
bool CheckIfNotHandledOptionsExists (int &argc, char **argv)
 
void CorrectRemainingOptions (int &argc, char **argv)
 
void AddCommand (const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
 
CommandFindCommand (const G4String &marker)
 
CommandGetCommandIfActive (const G4String &marker)
 
G4bool WereOptionsSetup ()
 
 CommandLineParser ()
 
 ~CommandLineParser ()
 
int Parse (int &argc, char **argv)
 
void PrintHelp ()
 
bool CheckIfNotHandledOptionsExists (int &argc, char **argv)
 
void CorrectRemainingOptions (int &argc, char **argv)
 
void AddCommand (const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
 
CommandFindCommand (const G4String &marker)
 
CommandGetCommandIfActive (const G4String &marker)
 
G4bool WereOptionsSetup ()
 
 CommandLineParser ()
 
 ~CommandLineParser ()
 
int Parse (int &argc, char **argv)
 
void PrintHelp ()
 
bool CheckIfNotHandledOptionsExists (int &argc, char **argv)
 
void CorrectRemainingOptions (int &argc, char **argv)
 
void AddCommand (const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
 
CommandFindCommand (const G4String &marker)
 
CommandGetCommandIfActive (const G4String &marker)
 
G4bool WereOptionsSetup ()
 
 CommandLineParser ()
 
 ~CommandLineParser ()
 
int Parse (int &argc, char **argv)
 
void PrintHelp ()
 
bool CheckIfNotHandledOptionsExists (int &argc, char **argv)
 
void CorrectRemainingOptions (int &argc, char **argv)
 
void AddCommand (const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
 
CommandFindCommand (const G4String &marker)
 
CommandGetCommandIfActive (const G4String &marker)
 
G4bool WereOptionsSetup ()
 
 CommandLineParser ()
 
 ~CommandLineParser ()
 
int Parse (int &argc, char **argv)
 
void PrintHelp ()
 
bool CheckIfNotHandledOptionsExists (int &argc, char **argv)
 
void CorrectRemainingOptions (int &argc, char **argv)
 
void AddCommand (const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
 
CommandFindCommand (const G4String &marker)
 
CommandGetCommandIfActive (const G4String &marker)
 
G4bool WereOptionsSetup ()
 
 CommandLineParser ()
 
 ~CommandLineParser ()
 
int Parse (int &argc, char **argv)
 
void PrintHelp ()
 
bool CheckIfNotHandledOptionsExists (int &argc, char **argv)
 
void CorrectRemainingOptions (int &argc, char **argv)
 
void AddCommand (const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
 
CommandFindCommand (const G4String &marker)
 
CommandGetCommandIfActive (const G4String &marker)
 
G4bool WereOptionsSetup ()
 

Static Public Member Functions

static CommandLineParserGetParser ()
 
static void DeleteInstance ()
 
static CommandLineParserGetParser ()
 
static void DeleteInstance ()
 
static CommandLineParserGetParser ()
 
static void DeleteInstance ()
 
static CommandLineParserGetParser ()
 
static void DeleteInstance ()
 
static CommandLineParserGetParser ()
 
static void DeleteInstance ()
 
static CommandLineParserGetParser ()
 
static void DeleteInstance ()
 
static CommandLineParserGetParser ()
 
static void DeleteInstance ()
 
static CommandLineParserGetParser ()
 
static void DeleteInstance ()
 

Detailed Description

Definition at line 106 of file CommandLineParser.hh.

Constructor & Destructor Documentation

CommandLineParser::CommandLineParser ( )

Definition at line 59 of file CommandLineParser.cc.

60 {
61  // G4cout << "############ NEW PARSE ##########" << G4endl;
62  fpInstance = this;
63  fOptionsWereSetup = false;
64  fMaxMarkerLength = 0;
65  fMaxOptionNameLength = 0;
66  AddCommand("--help", Command::WithoutOption, "Print this help");
67  AddCommand("-h", Command::WithoutOption, "Print this help");
69 
70  fVerbose = 0;
71 }
void AddCommand(const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
CommandLineParser::~CommandLineParser ( )

Definition at line 83 of file CommandLineParser.cc.

84 {
85  std::map<G4String, Command*>::iterator it = fCommandMap.begin();
86  for (; it != fCommandMap.end(); it++)
87  {
88  if (it->second) delete it->second;
89  }
90 }
G4DNAPARSER::CommandLineParser::CommandLineParser ( )
G4DNAPARSER::CommandLineParser::~CommandLineParser ( )
G4DNAPARSER::CommandLineParser::CommandLineParser ( )
G4DNAPARSER::CommandLineParser::~CommandLineParser ( )
G4DNAPARSER::CommandLineParser::CommandLineParser ( )
G4DNAPARSER::CommandLineParser::~CommandLineParser ( )
G4DNAPARSER::CommandLineParser::CommandLineParser ( )
G4DNAPARSER::CommandLineParser::~CommandLineParser ( )
G4DNAPARSER::CommandLineParser::CommandLineParser ( )
G4DNAPARSER::CommandLineParser::~CommandLineParser ( )
G4DNAPARSER::CommandLineParser::CommandLineParser ( )
G4DNAPARSER::CommandLineParser::~CommandLineParser ( )
G4DNAPARSER::CommandLineParser::CommandLineParser ( )
G4DNAPARSER::CommandLineParser::~CommandLineParser ( )

Member Function Documentation

void G4DNAPARSER::CommandLineParser::AddCommand ( const G4String marker,
Command::Type  ,
const G4String description = "",
const G4String defaultOption = "",
const G4String optionName = "" 
)
void CommandLineParser::AddCommand ( const G4String marker,
Command::Type  type,
const G4String description = "",
const G4String defaultOption = "",
const G4String optionName = "" 
)

Definition at line 274 of file CommandLineParser.cc.

279 {
280  // G4cout << "Add command : "<< marker << G4endl;
281 
282  Command* command = 0;
283  switch(type)
284  {
286  command = new Command(type, description);
287  break;
288 
289  default:
290  command = new CommandWithOption(type,
291  description,
292  defaultOption,
293  optionName);
294  if ((int) defaultOption.length() > fMaxOptionNameLength)
295  fMaxOptionNameLength = defaultOption.length();
296  break;
297  }
298 
299  if ((int) marker.length() > fMaxMarkerLength) fMaxMarkerLength =
300  marker.length();
301  fCommandMap.insert(make_pair(marker, command));
302 }

Here is the caller graph for this function:

void G4DNAPARSER::CommandLineParser::AddCommand ( const G4String marker,
Command::Type  ,
const G4String description = "",
const G4String defaultOption = "",
const G4String optionName = "" 
)
void G4DNAPARSER::CommandLineParser::AddCommand ( const G4String marker,
Command::Type  ,
const G4String description = "",
const G4String defaultOption = "",
const G4String optionName = "" 
)
void G4DNAPARSER::CommandLineParser::AddCommand ( const G4String marker,
Command::Type  ,
const G4String description = "",
const G4String defaultOption = "",
const G4String optionName = "" 
)
void G4DNAPARSER::CommandLineParser::AddCommand ( const G4String marker,
Command::Type  ,
const G4String description = "",
const G4String defaultOption = "",
const G4String optionName = "" 
)
void G4DNAPARSER::CommandLineParser::AddCommand ( const G4String marker,
Command::Type  ,
const G4String description = "",
const G4String defaultOption = "",
const G4String optionName = "" 
)
void G4DNAPARSER::CommandLineParser::AddCommand ( const G4String marker,
Command::Type  ,
const G4String description = "",
const G4String defaultOption = "",
const G4String optionName = "" 
)
bool G4DNAPARSER::CommandLineParser::CheckIfNotHandledOptionsExists ( int argc,
char **  argv 
)
bool G4DNAPARSER::CommandLineParser::CheckIfNotHandledOptionsExists ( int argc,
char **  argv 
)
bool G4DNAPARSER::CommandLineParser::CheckIfNotHandledOptionsExists ( int argc,
char **  argv 
)
bool G4DNAPARSER::CommandLineParser::CheckIfNotHandledOptionsExists ( int argc,
char **  argv 
)
bool CommandLineParser::CheckIfNotHandledOptionsExists ( int argc,
char **  argv 
)

Definition at line 375 of file CommandLineParser.cc.

376 {
377  if (argc > 0)
378  {
379  G4bool kill = false;
380  for (G4int i = 1; i < argc; i++)
381  {
382  if (strcmp(argv[i], ""))
383  {
384  kill = true;
385  G4cerr << "Unknown argument : " << argv[i] << "\n";
386  }
387  }
388  if (kill)
389  {
390  G4cerr << "The option " << argv[0]
391  << " is not handled this programme." << G4endl;
392  G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
393  PrintHelp();
394  return true; // KILL APPLICATION
395  }
396  }
397  return false;
398 }
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
#define G4endl
Definition: G4ios.hh:61
G4GLOB_DLL std::ostream G4cerr

Here is the call graph for this function:

Here is the caller graph for this function:

bool G4DNAPARSER::CommandLineParser::CheckIfNotHandledOptionsExists ( int argc,
char **  argv 
)
bool G4DNAPARSER::CommandLineParser::CheckIfNotHandledOptionsExists ( int argc,
char **  argv 
)
bool G4DNAPARSER::CommandLineParser::CheckIfNotHandledOptionsExists ( int argc,
char **  argv 
)
void G4DNAPARSER::CommandLineParser::CorrectRemainingOptions ( int argc,
char **  argv 
)
void G4DNAPARSER::CommandLineParser::CorrectRemainingOptions ( int argc,
char **  argv 
)
void G4DNAPARSER::CommandLineParser::CorrectRemainingOptions ( int argc,
char **  argv 
)
void G4DNAPARSER::CommandLineParser::CorrectRemainingOptions ( int argc,
char **  argv 
)
void G4DNAPARSER::CommandLineParser::CorrectRemainingOptions ( int argc,
char **  argv 
)
void G4DNAPARSER::CommandLineParser::CorrectRemainingOptions ( int argc,
char **  argv 
)
void G4DNAPARSER::CommandLineParser::CorrectRemainingOptions ( int argc,
char **  argv 
)
void CommandLineParser::CorrectRemainingOptions ( int argc,
char **  argv 
)

Definition at line 257 of file CommandLineParser.cc.

258 {
259  // remove handled arguments from argument array
260  int j = 0;
261  for (int i = 0; i < argc; i++)
262  {
263  if (strcmp(argv[i], ""))
264  {
265  argv[j] = argv[i];
266  j++;
267  }
268  }
269  argc = j;
270 }

Here is the caller graph for this function:

static void G4DNAPARSER::CommandLineParser::DeleteInstance ( )
static
static void G4DNAPARSER::CommandLineParser::DeleteInstance ( )
static
void CommandLineParser::DeleteInstance ( )
static

Definition at line 94 of file CommandLineParser.cc.

95 {
96  if (fpInstance)
97  {
98  delete fpInstance;
99  fpInstance = 0;
100  }
101 }

Here is the caller graph for this function:

static void G4DNAPARSER::CommandLineParser::DeleteInstance ( )
static
static void G4DNAPARSER::CommandLineParser::DeleteInstance ( )
static
static void G4DNAPARSER::CommandLineParser::DeleteInstance ( )
static
static void G4DNAPARSER::CommandLineParser::DeleteInstance ( )
static
static void G4DNAPARSER::CommandLineParser::DeleteInstance ( )
static
Command* G4DNAPARSER::CommandLineParser::FindCommand ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::FindCommand ( const G4String marker)
Command * CommandLineParser::FindCommand ( const G4String marker)

Definition at line 332 of file CommandLineParser.cc.

333 {
334  std::map<G4String, Command*>::iterator it = fCommandMap.find(marker);
335  if (it == fCommandMap.end())
336  {
337  // G4cerr << "command not found" << G4endl;
338  return 0;
339  }
340  return it->second;
341 }

Here is the caller graph for this function:

Command* G4DNAPARSER::CommandLineParser::FindCommand ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::FindCommand ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::FindCommand ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::FindCommand ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::FindCommand ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::GetCommandIfActive ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::GetCommandIfActive ( const G4String marker)
Command * CommandLineParser::GetCommandIfActive ( const G4String marker)

Definition at line 345 of file CommandLineParser.cc.

346 {
347  Command* command = FindCommand(marker);
348  if (command)
349  {
350  // G4cout << "Command found : "<< marker << G4endl;
351 
352  if (command->fActive)
353  {
354  // G4cout << "Command Active" << G4endl;
355  return command;
356  }
357  // else
358  // G4cout <<"Command not active" << G4endl;
359  }
360  else
361  {
362  G4ExceptionDescription description;
363  description << "You try to retrieve a command that was not registered : "
364  << marker << G4endl;
365  G4Exception("CommandLineParser::GetCommandIfActive",
366  "COMMAND LINE NOT DEFINED", FatalException, description, "");
367  // If you are using this class outside of Geant4, use exit(-1) instead
368  //exit(-1);
369  }
370  return 0;
371 }
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
#define G4endl
Definition: G4ios.hh:61
Command * FindCommand(const G4String &marker)

Here is the call graph for this function:

Here is the caller graph for this function:

Command* G4DNAPARSER::CommandLineParser::GetCommandIfActive ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::GetCommandIfActive ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::GetCommandIfActive ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::GetCommandIfActive ( const G4String marker)
Command* G4DNAPARSER::CommandLineParser::GetCommandIfActive ( const G4String marker)
static CommandLineParser* G4DNAPARSER::CommandLineParser::GetParser ( )
static
static CommandLineParser* G4DNAPARSER::CommandLineParser::GetParser ( )
static
static CommandLineParser* G4DNAPARSER::CommandLineParser::GetParser ( )
static
static CommandLineParser* G4DNAPARSER::CommandLineParser::GetParser ( )
static
static CommandLineParser* G4DNAPARSER::CommandLineParser::GetParser ( )
static
CommandLineParser * CommandLineParser::GetParser ( )
static

Definition at line 75 of file CommandLineParser.cc.

76 {
77  if (!fpInstance) new CommandLineParser;
78  return fpInstance;
79 }

Here is the caller graph for this function:

static CommandLineParser* G4DNAPARSER::CommandLineParser::GetParser ( )
static
static CommandLineParser* G4DNAPARSER::CommandLineParser::GetParser ( )
static
int G4DNAPARSER::CommandLineParser::Parse ( int argc,
char **  argv 
)
int G4DNAPARSER::CommandLineParser::Parse ( int argc,
char **  argv 
)
int G4DNAPARSER::CommandLineParser::Parse ( int argc,
char **  argv 
)
int G4DNAPARSER::CommandLineParser::Parse ( int argc,
char **  argv 
)
int G4DNAPARSER::CommandLineParser::Parse ( int argc,
char **  argv 
)
int G4DNAPARSER::CommandLineParser::Parse ( int argc,
char **  argv 
)
int G4DNAPARSER::CommandLineParser::Parse ( int argc,
char **  argv 
)
int CommandLineParser::Parse ( int argc,
char **  argv 
)

Definition at line 127 of file CommandLineParser.cc.

128 {
129  // G4cout << "Parse " << G4endl;
130  static char null[1] = { "" };
131  int firstArgc = argc;
132 
133  for (int i = 1; i < firstArgc; i++)
134  {
135  Command* command = FindCommand(argv[i]);
136  if (command == 0) continue;
137 
138  if (fVerbose) G4cout << "Command : " << argv[i] << G4endl;
139 
140  fOptionsWereSetup = true;
141  command->fActive = true;
142 
143  G4String marker(argv[i]);
144 
145  if (strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "--help") != 0)
146  {
147  argv[i] = null;
148  }
149 
150  if (command->fType == Command::WithOption)
151  {
152  if (fVerbose) G4cout << "WithOption" << G4endl;
153 
154  if(i+1 > firstArgc || argv[i+1]==0 || argv[i+1][0]=='-')
155  {
156  G4cerr << "An command line option is missing for "
157  << marker << G4endl;
158  abort();
159  }
160 
161  command->SetOption( (const char*) strdup(argv[i+1]) );
162  argv[i+1] = null;
163  i++;
164  }
165  else if(command->fType == Command::OptionNotCompulsory)
166  {
167  if(fVerbose)
168  G4cout <<"OptionNotCompulsory"<<G4endl;
169 
170  if(i+1 < firstArgc)
171  {
172  G4String buffer = (const char*) strdup(argv[i+1]);
173 
174  if(buffer.empty() == false)
175  {
176  if(buffer.at(0) != '-'
177  && buffer.at(0) != '&'
178  && buffer.at(0) != '>'
179  && buffer.at(0) != '|')
180  {
181  if(fVerbose)
182  {
183  G4cout << "facultative option is : " << buffer << G4endl;
184  }
185 
186  command->SetOption( (const char*) strdup(argv[i+1]) );
187  argv[i+1] = null;
188  i++;
189  continue;
190  }
191  }
192  }
193 
194  if(fVerbose)
195  G4cout << "Option not set" << G4endl;
196 
197  command->SetOption("");
198  }
199  }
200  CorrectRemainingOptions(argc, argv);
201 
202  Command* commandLine(0);
203  if ((commandLine = GetCommandIfActive("--help")) || (commandLine =
204  GetCommandIfActive("-h")))
205  {
206  G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
207  PrintHelp();
208  return 1;
209  }
210 
211  return 0;
212 }
Command * GetCommandIfActive(const G4String &marker)
virtual void SetOption(const G4String &)
#define buffer
Definition: xmlparse.cc:628
G4GLOB_DLL std::ostream G4cout
void CorrectRemainingOptions(int &argc, char **argv)
#define G4endl
Definition: G4ios.hh:61
Command * FindCommand(const G4String &marker)
G4GLOB_DLL std::ostream G4cerr

Here is the call graph for this function:

Here is the caller graph for this function:

void G4DNAPARSER::CommandLineParser::PrintHelp ( )
void G4DNAPARSER::CommandLineParser::PrintHelp ( )
void G4DNAPARSER::CommandLineParser::PrintHelp ( )
void CommandLineParser::PrintHelp ( )

Definition at line 216 of file CommandLineParser.cc.

217 {
218  std::map<G4String, Command*>::iterator it;
219 
220  int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4;
221 
222  G4cout << "Options: " << G4endl;
223 
224  for (it = fCommandMap.begin(); it != fCommandMap.end(); it++)
225  {
226  Command* command = it->second;
227  if (command)
228  {
229  G4cout << setw(maxFieldLength) << left;
230 
231  G4String toPrint = it->first;
232 
233  if (toPrint == "&")
234  {
235  continue;
236  }
237  else if (toPrint == "-h") continue;
238  else if (toPrint == "--help")
239  {
240  toPrint += ", -h";
241  }
242 
243  if (command->GetDefaultOption() != "")
244  {
245  toPrint += " \"" + command->GetDefaultOption() + "\"";
246  }
247 
248  G4cout << toPrint;
249 
250  G4cout << command->GetDescription() << G4endl;
251  }
252  }
253 }
virtual const G4String & GetDefaultOption()
G4int first(char) const
const G4String & GetDescription()
G4GLOB_DLL std::ostream G4cout
#define G4endl
Definition: G4ios.hh:61

Here is the call graph for this function:

Here is the caller graph for this function:

void G4DNAPARSER::CommandLineParser::PrintHelp ( )
void G4DNAPARSER::CommandLineParser::PrintHelp ( )
void G4DNAPARSER::CommandLineParser::PrintHelp ( )
void G4DNAPARSER::CommandLineParser::PrintHelp ( )
G4bool G4DNAPARSER::CommandLineParser::WereOptionsSetup ( )
inline

Definition at line 123 of file CommandLineParser.hh.

123 {return fOptionsWereSetup;}
G4bool G4DNAPARSER::CommandLineParser::WereOptionsSetup ( )
inline

Definition at line 130 of file CommandLineParser.hh.

130 {return fOptionsWereSetup;}
G4bool G4DNAPARSER::CommandLineParser::WereOptionsSetup ( )
inline

Definition at line 130 of file CommandLineParser.hh.

130 {return fOptionsWereSetup;}
G4bool G4DNAPARSER::CommandLineParser::WereOptionsSetup ( )
inline

Definition at line 130 of file CommandLineParser.hh.

130 {return fOptionsWereSetup;}
G4bool G4DNAPARSER::CommandLineParser::WereOptionsSetup ( )
inline

Definition at line 130 of file CommandLineParser.hh.

130 {return fOptionsWereSetup;}
G4bool G4DNAPARSER::CommandLineParser::WereOptionsSetup ( )
inline

Definition at line 130 of file CommandLineParser.hh.

130 {return fOptionsWereSetup;}
G4bool G4DNAPARSER::CommandLineParser::WereOptionsSetup ( )
inline

Definition at line 130 of file CommandLineParser.hh.

130 {return fOptionsWereSetup;}
G4bool G4DNAPARSER::CommandLineParser::WereOptionsSetup ( )
inline

Definition at line 130 of file CommandLineParser.hh.

130 {return fOptionsWereSetup;}

The documentation for this class was generated from the following files: