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 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /examples/extended/medical/dna/chem4/src/CommandLineParser.cc (Version 11.3.0) and /examples/extended/medical/dna/chem4/src/CommandLineParser.cc (Version 8.1)


  1 //                                                  1 
  2 // *******************************************    
  3 // * License and Disclaimer                       
  4 // *                                              
  5 // * The  Geant4 software  is  copyright of th    
  6 // * the Geant4 Collaboration.  It is provided    
  7 // * conditions of the Geant4 Software License    
  8 // * LICENSE and available at  http://cern.ch/    
  9 // * include a list of copyright holders.         
 10 // *                                              
 11 // * Neither the authors of this software syst    
 12 // * institutes,nor the agencies providing fin    
 13 // * work  make  any representation or  warran    
 14 // * regarding  this  software system or assum    
 15 // * use.  Please see the license in the file     
 16 // * for the full disclaimer and the limitatio    
 17 // *                                              
 18 // * This  code  implementation is the result     
 19 // * technical work of the GEANT4 collaboratio    
 20 // * By using,  copying,  modifying or  distri    
 21 // * any work based  on the software)  you  ag    
 22 // * use  in  resulting  scientific  publicati    
 23 // * acceptance of all terms of the Geant4 Sof    
 24 // *******************************************    
 25 //                                                
 26 // This example is provided by the Geant4-DNA     
 27 // Any report or published results obtained us    
 28 // shall cite the following Geant4-DNA collabo    
 29 // Med. Phys. 37 (2010) 4692-4708                 
 30 // J. Comput. Phys. 274 (2014) 841-882            
 31 // The Geant4-DNA web site is available at htt    
 32 //                                                
 33 // Author: Mathieu Karamitros                     
 34 //                                                
 35 //                                                
 36 /// \file CommandLineParser.cc                    
 37 /// \brief Implementation of the CommandLinePa    
 38                                                   
 39 #include "CommandLineParser.hh"                   
 40                                                   
 41 #include <iomanip>                                
 42                                                   
 43 using namespace std;                              
 44 using namespace G4DNAPARSER;                      
 45                                                   
 46 CommandLineParser* CommandLineParser::fpInstan    
 47 G4String Command::fNoOption = "NoOption";         
 48                                                   
 49 //....oooOO0OOooo........oooOO0OOooo........oo    
 50                                                   
 51 inline bool MATCH(const char* a, const char* b    
 52 {                                                 
 53   return strcmp(a, b) == 0;                       
 54 }                                                 
 55                                                   
 56 //....oooOO0OOooo........oooOO0OOooo........oo    
 57                                                   
 58 CommandLineParser::CommandLineParser()            
 59 {                                                 
 60   // G4cout << "############ NEW PARSE #######    
 61   fpInstance = this;                              
 62   fOptionsWereSetup = false;                      
 63   fMaxMarkerLength = 0;                           
 64   fMaxOptionNameLength = 0;                       
 65   AddCommand("--help", Command::WithoutOption,    
 66   AddCommand("-h", Command::WithoutOption, "Pr    
 67   AddCommand("&", Command::WithoutOption);        
 68                                                   
 69   fVerbose = 0;                                   
 70 }                                                 
 71                                                   
 72 //....oooOO0OOooo........oooOO0OOooo........oo    
 73                                                   
 74 CommandLineParser* CommandLineParser::GetParse    
 75 {                                                 
 76   if (!fpInstance) new CommandLineParser;         
 77   return fpInstance;                              
 78 }                                                 
 79                                                   
 80 //....oooOO0OOooo........oooOO0OOooo........oo    
 81                                                   
 82 CommandLineParser::~CommandLineParser()           
 83 {                                                 
 84   std::map<G4String, Command*>::iterator it =     
 85   for (; it != fCommandMap.end(); it++) {         
 86     if (it->second) delete it->second;            
 87   }                                               
 88 }                                                 
 89                                                   
 90 //....oooOO0OOooo........oooOO0OOooo........oo    
 91                                                   
 92 void CommandLineParser::DeleteInstance()          
 93 {                                                 
 94   if (fpInstance) {                               
 95     delete fpInstance;                            
 96     fpInstance = 0;                               
 97   }                                               
 98 }                                                 
 99                                                   
100 //....oooOO0OOooo........oooOO0OOooo........oo    
101                                                   
102 Command::Command(Command::Type commandType, co    
103 {                                                 
104   fType = commandType;                            
105   fDescription = description;                     
106   fActive = false;                                
107 }                                                 
108                                                   
109 //....oooOO0OOooo........oooOO0OOooo........oo    
110                                                   
111 CommandWithOption::CommandWithOption(Command::    
112                                      const G4S    
113   : Command(commandType, description)             
114 {                                                 
115   fDefaultOption = defaultOption;                 
116   fOptionName = optionName;                       
117   fOption = "";                                   
118 }                                                 
119                                                   
120 //....oooOO0OOooo........oooOO0OOooo........oo    
121                                                   
122 int CommandLineParser::Parse(int& argc, char**    
123 {                                                 
124   //    G4cout << "Parse " << G4endl;             
125   static char null[1] = {""};                     
126   int firstArgc = argc;                           
127                                                   
128   for (int i = 1; i < firstArgc; i++) {           
129     Command* command = FindCommand(argv[i]);      
130     if (command == 0) continue;                   
131                                                   
132     if (fVerbose) G4cout << "Command : " << ar    
133                                                   
134     fOptionsWereSetup = true;                     
135     command->fActive = true;                      
136                                                   
137     G4String marker(argv[i]);                     
138                                                   
139     if (strcmp(argv[i], "-h") != 0 && strcmp(a    
140       argv[i] = null;                             
141     }                                             
142                                                   
143     if (command->fType == Command::WithOption)    
144       if (fVerbose) G4cout << "WithOption" <<     
145                                                   
146       if (i + 1 > firstArgc || argv[i + 1] ==     
147         G4cerr << "An command line option is m    
148         abort();                                  
149       }                                           
150                                                   
151       command->SetOption((const char*)strdup(a    
152       argv[i + 1] = null;                         
153       i++;                                        
154     }                                             
155     else if (command->fType == Command::Option    
156       if (fVerbose) G4cout << "OptionNotCompul    
157                                                   
158       if (i + 1 < firstArgc) {                    
159         G4String buffer = (const char*)strdup(    
160                                                   
161         if (buffer.empty() == false) {            
162           if (buffer.at(0) != '-' && buffer.at    
163               && buffer.at(0) != '|')             
164           {                                       
165             if (fVerbose) {                       
166               G4cout << "facultative option is    
167             }                                     
168                                                   
169             command->SetOption((const char*)st    
170             argv[i + 1] = null;                   
171             i++;                                  
172             continue;                             
173           }                                       
174         }                                         
175       }                                           
176                                                   
177       if (fVerbose) G4cout << "Option not set"    
178                                                   
179       command->SetOption("");                     
180     }                                             
181   }                                               
182   CorrectRemainingOptions(argc, argv);            
183                                                   
184   Command* commandLine(0);                        
185   if ((commandLine = GetCommandIfActive("--hel    
186     G4cout << "Usage : " << argv[0] << " [OPTI    
187     PrintHelp();                                  
188     return 1;                                     
189   }                                               
190                                                   
191   return 0;                                       
192 }                                                 
193                                                   
194 //....oooOO0OOooo........oooOO0OOooo........oo    
195                                                   
196 void CommandLineParser::PrintHelp()               
197 {                                                 
198   std::map<G4String, Command*>::iterator it;      
199                                                   
200   int maxFieldLength = fMaxMarkerLength + fMax    
201                                                   
202   G4cout << "Options: " << G4endl;                
203                                                   
204   for (it = fCommandMap.begin(); it != fComman    
205     Command* command = it->second;                
206     if (command) {                                
207       G4cout << setw(maxFieldLength) << left;     
208                                                   
209       G4String toPrint = it->first;               
210                                                   
211       if (toPrint == "&") {                       
212         continue;                                 
213       }                                           
214       else if (toPrint == "-h")                   
215         continue;                                 
216       else if (toPrint == "--help") {             
217         toPrint += ", -h";                        
218       }                                           
219                                                   
220       if (command->GetDefaultOption() != "") {    
221         toPrint += " \"" + command->GetDefault    
222       }                                           
223                                                   
224       G4cout << toPrint;                          
225                                                   
226       G4cout << command->GetDescription() << G    
227     }                                             
228   }                                               
229 }                                                 
230                                                   
231 //....oooOO0OOooo........oooOO0OOooo........oo    
232                                                   
233 void CommandLineParser::CorrectRemainingOption    
234 {                                                 
235   // remove handled arguments from argument ar    
236   int j = 0;                                      
237   for (int i = 0; i < argc; i++) {                
238     if (strcmp(argv[i], "")) {                    
239       argv[j] = argv[i];                          
240       j++;                                        
241     }                                             
242   }                                               
243   argc = j;                                       
244 }                                                 
245                                                   
246 //....oooOO0OOooo........oooOO0OOooo........oo    
247                                                   
248 void CommandLineParser::AddCommand(const G4Str    
249                                    const G4Str    
250                                    const G4Str    
251 {                                                 
252   // G4cout << "Add command : "<< marker << G4    
253                                                   
254   Command* command = 0;                           
255   switch (type) {                                 
256     case Command::WithoutOption:                  
257       command = new Command(type, description)    
258       break;                                      
259                                                   
260     default:                                      
261       command = new CommandWithOption(type, de    
262       if ((int)defaultOption.length() > fMaxOp    
263         fMaxOptionNameLength = defaultOption.l    
264       break;                                      
265   }                                               
266                                                   
267   if ((int)marker.length() > fMaxMarkerLength)    
268   fCommandMap.insert(make_pair(marker, command    
269 }                                                 
270                                                   
271 //....oooOO0OOooo........oooOO0OOooo........oo    
272                                                   
273 /*                                                
274 // Add one command but multiple markers           
275 void Parser::AddCommand(vector<G4String> marke    
276                         CommandType type,         
277                         const G4String& descri    
278                         const G4String& option    
279 {                                                 
280   // G4cout << "Add command : "<< marker << G4    
281   Command* command = new Command(type, descrip    
282                                                   
283   for (size_t i = 0; i < markers.size; i++)       
284   {                                               
285     G4String marker = markers[i];                 
286     if ((int) marker.length() > fMaxMarkerLeng    
287     {                                             
288       fMaxMarkerLength = marker.length();         
289     }                                             
290     if ((int) optionName.length() > fMaxOption    
291     {                                             
292       fMaxOptionNameLength = optionName.length    
293     }                                             
294     fCommandMap.insert(make_pair(marker, comma    
295   }                                               
296 }                                                 
297 */                                                
298                                                   
299 //....oooOO0OOooo........oooOO0OOooo........oo    
300                                                   
301 Command* CommandLineParser::FindCommand(const     
302 {                                                 
303   std::map<G4String, Command*>::iterator it =     
304   if (it == fCommandMap.end()) {                  
305     // G4cerr << "command not found" << G4endl    
306     return 0;                                     
307   }                                               
308   return it->second;                              
309 }                                                 
310                                                   
311 //....oooOO0OOooo........oooOO0OOooo........oo    
312                                                   
313 Command* CommandLineParser::GetCommandIfActive    
314 {                                                 
315   Command* command = FindCommand(marker);         
316   if (command) {                                  
317     // G4cout << "Command found : "<< marker <    
318                                                   
319     if (command->fActive) {                       
320       // G4cout << "Command Active" << G4endl;    
321       return command;                             
322     }                                             
323     // else                                       
324     //  G4cout <<"Command not active" << G4end    
325   }                                               
326   else {                                          
327     G4ExceptionDescription description;           
328     description << "You try to retrieve a comm    
329     G4Exception("CommandLineParser::GetCommand    
330                 description, "");                 
331     // If you are using this class outside of     
332     // exit(-1);                                  
333   }                                               
334   return 0;                                       
335 }                                                 
336                                                   
337 //....oooOO0OOooo........oooOO0OOooo........oo    
338                                                   
339 bool CommandLineParser::CheckIfNotHandledOptio    
340 {                                                 
341   if (argc > 0) {                                 
342     G4bool kill = false;                          
343     for (G4int i = 1; i < argc; i++) {            
344       if (strcmp(argv[i], "")) {                  
345         kill = true;                              
346         G4cerr << "Unknown argument : " << arg    
347       }                                           
348     }                                             
349     if (kill) {                                   
350       G4cerr << "The option " << argv[0] << "     
351       G4cout << "Usage : " << argv[0] << " [OP    
352       PrintHelp();                                
353       return true;  // KILL APPLICATION           
354     }                                             
355   }                                               
356   return false;                                   
357 }                                                 
358