Geant4  10.03
CommandLineParser.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // This example is provided by the Geant4-DNA collaboration
27 // Any report or published results obtained using the Geant4-DNA software
28 // shall cite the following Geant4-DNA collaboration publication:
29 // Med. Phys. 37 (2010) 4692-4708
30 // J. Comput. Phys. 274 (2014) 841-882
31 // The Geant4-DNA web site is available at http://geant4-dna.org
32 //
33 // Author: Mathieu Karamitros
34 //
35 // $Id$
36 //
39 
40 #include <iomanip>
41 #include "CommandLineParser.hh"
42 
43 using namespace std;
44 using namespace G4DNAPARSER;
45 
46 CommandLineParser* CommandLineParser::fpInstance(0);
47 G4String Command::fNoOption = "NoOption";
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
51 inline bool MATCH(const char *a, const char *b)
52 {
53  return strcmp(a, b) == 0;
54 }
55 
56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
57 
58 CommandLineParser::CommandLineParser()
59 {
60  // G4cout << "############ NEW PARSE ##########" << G4endl;
61  fpInstance = this;
62  fOptionsWereSetup = false;
63  fMaxMarkerLength = 0;
64  fMaxOptionNameLength = 0;
65  AddCommand("--help", Command::WithoutOption, "Print this help");
66  AddCommand("-h", Command::WithoutOption, "Print this help");
67  AddCommand("&", Command::WithoutOption);
68 
69  fVerbose = 0;
70 }
71 
72 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
73 
74 CommandLineParser* CommandLineParser::GetParser()
75 {
76  if (!fpInstance) new CommandLineParser;
77  return fpInstance;
78 }
79 
80 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
81 
82 CommandLineParser::~CommandLineParser()
83 {
84  std::map<G4String, Command*>::iterator it = fCommandMap.begin();
85  for (; it != fCommandMap.end(); it++)
86  {
87  if (it->second) delete it->second;
88  }
89 }
90 
91 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
92 
93 void CommandLineParser::DeleteInstance()
94 {
95  if (fpInstance)
96  {
97  delete fpInstance;
98  fpInstance = 0;
99  }
100 }
101 
102 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
103 
104 Command::Command(Command::Type commandType,
105  const G4String& description)
106 {
107  fType = commandType;
108  fDescription = description;
109  fActive = false;
110 }
111 
112 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
113 
114 CommandWithOption::CommandWithOption(Command::Type commandType,
115  const G4String& description,
116  const G4String& defaultOption,
117  const G4String& optionName) :
118 Command(commandType, description)
119 {
120  fDefaultOption = defaultOption;
121  fOptionName = optionName;
122  fOption = "";
123 }
124 
125 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
126 
127 int CommandLineParser::Parse(int& argc, char **argv)
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 }
213 
214 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
215 
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 }
254 
255 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
256 
257 void CommandLineParser::CorrectRemainingOptions(int& argc, char **argv)
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 }
271 
272 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
273 
274 void CommandLineParser::AddCommand(const G4String& marker,
275  Command::Type type,
276  const G4String& description,
277  const G4String& defaultOption,
278  const G4String& optionName)
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 }
303 
304 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
305 
306 /*
307 // Add one command but multiple markers
308 void Parser::AddCommand(vector<G4String> markers,
309  CommandType type,
310  const G4String& description,
311  const G4String& optionName)
312 {
313  // G4cout << "Add command : "<< marker << G4endl;
314  Command* command = new Command(type, description, optionName);
315 
316  for (size_t i = 0; i < markers.size; i++)
317  {
318  G4String marker = markers[i];
319  if ((int) marker.length() > fMaxMarkerLength)
320  {
321  fMaxMarkerLength = marker.length();
322  }
323  if ((int) optionName.length() > fMaxOptionNameLength)
324  {
325  fMaxOptionNameLength = optionName.length();
326  }
327  fCommandMap.insert(make_pair(marker, command));
328  }
329 }
330 */
331 
332 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
333 
335 {
336  std::map<G4String, Command*>::iterator it = fCommandMap.find(marker);
337  if (it == fCommandMap.end())
338  {
339  // G4cerr << "command not found" << G4endl;
340  return 0;
341  }
342  return it->second;
343 }
344 
345 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
346 
348 {
349  Command* command = FindCommand(marker);
350  if (command)
351  {
352  // G4cout << "Command found : "<< marker << G4endl;
353 
354  if (command->fActive)
355  {
356  // G4cout << "Command Active" << G4endl;
357  return command;
358  }
359  // else
360  // G4cout <<"Command not active" << G4endl;
361  }
362  else
363  {
364  G4ExceptionDescription description;
365  description << "You try to retrieve a command that was not registered : "
366  << marker << G4endl;
367  G4Exception("CommandLineParser::GetCommandIfActive",
368  "COMMAND LINE NOT DEFINED", FatalException, description, "");
369  // If you are using this class outside of Geant4, use exit(-1) instead
370  //exit(-1);
371  }
372  return 0;
373 }
374 
375 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
376 
377 bool CommandLineParser::CheckIfNotHandledOptionsExists(int& argc, char** argv)
378 {
379  if (argc > 0)
380  {
381  G4bool kill = false;
382  for (G4int i = 1; i < argc; i++)
383  {
384  if (strcmp(argv[i], ""))
385  {
386  kill = true;
387  G4cerr << "Unknown argument : " << argv[i] << "\n";
388  }
389  }
390  if (kill)
391  {
392  G4cerr << "The option " << argv[0]
393  << " is not handled this programme." << G4endl;
394  G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
395  PrintHelp();
396  return true; // KILL APPLICATION
397  }
398  }
399  return false;
400 }
void Parse(int &argc, char **argv)
Definition: chem1.cc:193
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
virtual const G4String & GetDefaultOption()
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
G4int first(char) const
void AddCommand(const G4String &marker, Command::Type, const G4String &description="", const G4String &defaultOption="", const G4String &optionName="")
Command * GetCommandIfActive(const G4String &marker)
const G4String & GetDescription()
virtual void SetOption(const G4String &)
bool CheckIfNotHandledOptionsExists(int &argc, char **argv)
#define buffer
Definition: xmlparse.cc:628
int G4int
Definition: G4Types.hh:78
std::map< G4String, Command * > fCommandMap
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void CorrectRemainingOptions(int &argc, char **argv)
bool MATCH(const char *a, const char *b)
#define G4endl
Definition: G4ios.hh:61
Command * FindCommand(const G4String &marker)
G4GLOB_DLL std::ostream G4cerr