Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 CommandWithOption::CommandWithOption(Command::Type commandType,
113  const G4String& description,
114  const G4String& defaultOption,
115  const G4String& optionName) :
116 Command(commandType, description)
117 {
118  fDefaultOption = defaultOption;
119  fOptionName = optionName;
120  fOption = "";
121 }
122 
123 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
124 
125 int CommandLineParser::Parse(int& argc, char **argv)
126 {
127  // G4cout << "Parse " << G4endl;
128  static char null[1] = { "" };
129  int firstArgc = argc;
130 
131  for (int i = 1; i < firstArgc; i++)
132  {
133  Command* command = FindCommand(argv[i]);
134  if (command == 0) continue;
135 
136  if (fVerbose) G4cout << "Command : " << argv[i] << G4endl;
137 
138  fOptionsWereSetup = true;
139  command->fActive = true;
140 
141  G4String marker(argv[i]);
142 
143  if (strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "--help") != 0)
144  {
145  argv[i] = null;
146  }
147 
148  if (command->fType == Command::WithOption)
149  {
150  if (fVerbose) G4cout << "WithOption" << G4endl;
151 
152  if(i+1 > firstArgc || argv[i+1]==0 || argv[i+1][0]=='-')
153  {
154  G4cerr << "An command line option is missing for "
155  << marker << G4endl;
156  abort();
157  }
158 
159  command->SetOption( (const char*) strdup(argv[i+1]) );
160  argv[i+1] = null;
161  i++;
162  }
163  else if(command->fType == Command::OptionNotCompulsory)
164  {
165  if(fVerbose)
166  G4cout <<"OptionNotCompulsory"<<G4endl;
167 
168  if(i+1 < firstArgc)
169  {
170  G4String buffer = (const char*) strdup(argv[i+1]);
171 
172  if(buffer.empty() == false)
173  {
174  if(buffer.at(0) != '-'
175  && buffer.at(0) != '&'
176  && buffer.at(0) != '>'
177  && buffer.at(0) != '|')
178  {
179  if(fVerbose)
180  {
181  G4cout << "facultative option is : " << buffer << G4endl;
182  }
183 
184  command->SetOption( (const char*) strdup(argv[i+1]) );
185  argv[i+1] = null;
186  i++;
187  continue;
188  }
189  }
190  }
191 
192  if(fVerbose)
193  G4cout << "Option not set" << G4endl;
194 
195  command->SetOption("");
196  }
197  }
198  CorrectRemainingOptions(argc, argv);
199 
200  Command* commandLine(0);
201  if ((commandLine = GetCommandIfActive("--help")) || (commandLine =
202  GetCommandIfActive("-h")))
203  {
204  G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
205  PrintHelp();
206  return 1;
207  }
208 
209  return 0;
210 }
211 
212 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
213 
215 {
216  std::map<G4String, Command*>::iterator it;
217 
218  int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4;
219 
220  G4cout << "Options: " << G4endl;
221 
222  for (it = fCommandMap.begin(); it != fCommandMap.end(); it++)
223  {
224  Command* command = it->second;
225  if (command)
226  {
227  G4cout << setw(maxFieldLength) << left;
228 
229  G4String toPrint = it->first;
230 
231  if (toPrint == "&")
232  {
233  continue;
234  }
235  else if (toPrint == "-h") continue;
236  else if (toPrint == "--help")
237  {
238  toPrint += ", -h";
239  }
240 
241  if (command->GetDefaultOption() != "")
242  {
243  toPrint += " \"" + command->GetDefaultOption() + "\"";
244  }
245 
246  G4cout << toPrint;
247 
248  G4cout << command->GetDescription() << G4endl;
249  }
250  }
251 }
252 
253 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
254 
255 void CommandLineParser::CorrectRemainingOptions(int& argc, char **argv)
256 {
257  // remove handled arguments from argument array
258  int j = 0;
259  for (int i = 0; i < argc; i++)
260  {
261  if (strcmp(argv[i], ""))
262  {
263  argv[j] = argv[i];
264  j++;
265  }
266  }
267  argc = j;
268 }
269 
270 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
271 
272 void CommandLineParser::AddCommand(const G4String& marker,
273  Command::Type type,
274  const G4String& description,
275  const G4String& defaultOption,
276  const G4String& optionName)
277 {
278  // G4cout << "Add command : "<< marker << G4endl;
279 
280  Command* command = 0;
281  switch(type)
282  {
284  command = new Command(type, description);
285  break;
286 
287  default:
288  command = new CommandWithOption(type,
289  description,
290  defaultOption,
291  optionName);
292  if ((int) defaultOption.length() > fMaxOptionNameLength)
293  fMaxOptionNameLength = defaultOption.length();
294  break;
295  }
296 
297  if ((int) marker.length() > fMaxMarkerLength) fMaxMarkerLength =
298  marker.length();
299  fCommandMap.insert(make_pair(marker, command));
300 }
301 
302 /*
303 // Add one command but multiple markers
304 void Parser::AddCommand(vector<G4String> markers,
305  CommandType type,
306  const G4String& description,
307  const G4String& optionName)
308 {
309  // G4cout << "Add command : "<< marker << G4endl;
310  Command* command = new Command(type, description, optionName);
311 
312  for (size_t i = 0; i < markers.size; i++)
313  {
314  G4String marker = markers[i];
315  if ((int) marker.length() > fMaxMarkerLength)
316  {
317  fMaxMarkerLength = marker.length();
318  }
319  if ((int) optionName.length() > fMaxOptionNameLength)
320  {
321  fMaxOptionNameLength = optionName.length();
322  }
323  fCommandMap.insert(make_pair(marker, command));
324  }
325 }
326 */
327 
328 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
329 
331 {
332  std::map<G4String, Command*>::iterator it = fCommandMap.find(marker);
333  if (it == fCommandMap.end())
334  {
335  // G4cerr << "command not found" << G4endl;
336  return 0;
337  }
338  return it->second;
339 }
340 
341 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
342 
344 {
345  Command* command = FindCommand(marker);
346  if (command)
347  {
348  // G4cout << "Command found : "<< marker << G4endl;
349 
350  if (command->fActive)
351  {
352  // G4cout << "Command Active" << G4endl;
353  return command;
354  }
355  // else
356  // G4cout <<"Command not active" << G4endl;
357  }
358  else
359  {
360  G4ExceptionDescription description;
361  description << "You try to retrieve a command that was not registered : "
362  << marker << G4endl;
363  G4Exception("CommandLineParser::GetCommandIfActive",
364  "COMMAND LINE NOT DEFINED", FatalException, description, "");
365  // If you are using this class outside of Geant4, use exit(-1) instead
366  //exit(-1);
367  }
368  return 0;
369 }
370 
371 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
372 
373 bool CommandLineParser::CheckIfNotHandledOptionsExists(int& argc, char** argv)
374 {
375  if (argc > 0)
376  {
377  G4bool kill = false;
378  for (G4int i = 1; i < argc; i++)
379  {
380  if (strcmp(argv[i], ""))
381  {
382  kill = true;
383  G4cerr << "Unknown argument : " << argv[i] << "\n";
384  }
385  }
386  if (kill)
387  {
388  G4cerr << "The option " << argv[0]
389  << " is not handled this programme." << G4endl;
390  G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
391  PrintHelp();
392  return true; // KILL APPLICATION
393  }
394  }
395  return false;
396 }
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
tuple b
Definition: test.py:12
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)
Definition: inflate.h:45
#define G4endl
Definition: G4ios.hh:61
Command * FindCommand(const G4String &marker)
G4GLOB_DLL std::ostream G4cerr