GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CmdLnArgParser.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2024, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <c.andreopoulos \at cern.ch>
7  University of Liverpool
8 */
9 //____________________________________________________________________________
10 
11 #include <cctype>
12 #include <cstdlib>
13 
17 
18 using namespace genie;
19 using namespace genie::utils;
20 
21 //____________________________________________________________________________
22 CmdLnArgParser::CmdLnArgParser(int argc, char **argv) :
23 fArgc(argc),
24 fArgv(argv)
25 {
26 
27 }
28 //____________________________________________________________________________
30 {
31 
32 }
33 //____________________________________________________________________________
34 char * CmdLnArgParser::Arg(char op)
35 {
36  const int buf_size = 2048*128;
37 
38  bool set = false;
39  char * argument = new char[buf_size];
40  strcpy(argument, "");
41 
42  int argc = fArgc;
43  char ** argv = fArgv;
44 
45  while(argc>2)
46  {
47  LOG("CLAP", pDEBUG) << "Getting next argument in argument-list";
48  LOG("CLAP", pDEBUG) << "Current argc = " << argc;
49 
50  if (argv[1][0] == '-') {
51  LOG("CLAP", pDEBUG)
52  << "Got char (argv[1][1]) following argv[1][0]='-' : " << argv[1][1];
53 
54  if (argv[1][1] == op) {
55  LOG("CLAP", pDEBUG) << "Input option: " << op << " was matched";
56 
57  if (strlen(&argv[1][2]) ) {
58  strcpy(argument,&argv[1][2]);
59  LOG("CLAP", pINFO)
60  << "Set opt = [" << op << "] to val = [" << argument << "]";
61 
62  } else if( (argc>2) &&
63  !(argv[2][0]=='-' && isalpha(argv[2][1])) ) {
64 
65  LOG("CLAP", pDEBUG)
66  << "argc>2 and next arg not a '-' followed by an alpha char";
67 
68  argc--;
69  argv++;
70  strcpy(argument,&argv[1][0]);
71  set = true;
72  LOG("CLAP", pINFO)
73  << "Set opt = [" << op << "] to val = [" << argument << "]";
74  }
75  }
76  }
77  argc--;
78  argv++;
79  if(argc>2) {
80  LOG("CLAP", pDEBUG) << "Next argv[1][0] = " << argv[1][0];
81  }
82  }
83 
84  LOG("CLAP", pDEBUG) << "CmdLnArgParser::Arg op='" << op << "' set=" << set;
85  return argument;
86 }
87 //____________________________________________________________________________
89 {
90  bool set = false;
91 
92  int argc = fArgc;
93  char ** argv = fArgv;
94 
95  while(argc>1) {
96  if(argv[1][0] == '-') {
97  if (argv[1][1] == op) set = true;
98  }
99  argc--;
100  argv++;
101  }
102 
103  return set;
104 }
105 //____________________________________________________________________________
107 {
108  char * argument = this->Arg(op);
109  string value = string(argument);
110  delete [] argument;
111 
112  return value;
113 }
114 //____________________________________________________________________________
115 vector<string> CmdLnArgParser::ArgAsStringTokens(char op, string delimeter)
116 {
117  string argument = this->ArgAsString(op);
118 
119  vector<string> tokens = str::Split(argument, delimeter);
120  return tokens;
121 }
122 //____________________________________________________________________________
124 {
125  char * argument = this->Arg(op);
126  double value = atof(argument);
127  delete [] argument;
128 
129  return value;
130 }
131 //____________________________________________________________________________
132 vector<double> CmdLnArgParser::ArgAsDoubleTokens(char op, string delimeter)
133 {
134  vector<string> strtokens = this->ArgAsStringTokens(op, delimeter);
135  vector<double> tokens;
136  vector<string>::const_iterator iter = strtokens.begin();
137  for( ; iter != strtokens.end(); ++iter) {
138  string arg = *iter;
139  tokens.push_back(atof(arg.c_str()));
140  }
141  return tokens;
142 }
143 //____________________________________________________________________________
145 {
146  char * argument = this->Arg(op);
147  int value = atoi(argument);
148  delete [] argument;
149 
150  return value;
151 }
152 //____________________________________________________________________________
153 vector<int> CmdLnArgParser::ArgAsIntTokens(char op, string delimeter)
154 {
155  vector<string> strtokens = this->ArgAsStringTokens(op, delimeter);
156  vector<int> tokens;
157  vector<string>::const_iterator iter = strtokens.begin();
158  for( ; iter != strtokens.end(); ++iter) {
159  string arg = *iter;
160  tokens.push_back(atoi(arg.c_str()));
161  }
162  return tokens;
163 }
164 //____________________________________________________________________________
166 {
167  char * argument = this->Arg(op);
168  long value = atol(argument);
169  delete [] argument;
170 
171  return value;
172 }
173 //____________________________________________________________________________
174 vector<long> CmdLnArgParser::ArgAsLongTokens(char op, string delimeter)
175 {
176  vector<string> strtokens = this->ArgAsStringTokens(op, delimeter);
177  vector<long> tokens;
178  vector<string>::const_iterator iter = strtokens.begin();
179  for( ; iter != strtokens.end(); ++iter) {
180  string arg = *iter;
181  tokens.push_back(atol(arg.c_str()));
182  }
183  return tokens;
184 }
185 //____________________________________________________________________________
186 char * CmdLnArgParser::Arg(string op)
187 {
188  const int buf_size = 2048*128;
189  char * argument = new char[buf_size];
190  strcpy(argument, "");
191 
192  int argc = fArgc;
193  char ** argv = fArgv;
194 
195  while(argc>2)
196  {
197  LOG("CLAP", pDEBUG) << "Getting next argument in argument-list";
198  LOG("CLAP", pDEBUG) << "Current argc = " << argc;
199 
200  if (argv[1][0] == '-' && argv[1][1] == '-') {
201  //char * op_cur = strndup(argv[1]+2,strlen(argv[1]));
202  char op_cur[buf_size];
203  strcpy(op_cur,&argv[1][2]);
204  LOG("CLAP", pDEBUG)
205  << "Got string following '--' : " << op_cur;
206  if (strcmp(op.c_str(),op_cur)==0) {
207  LOG("CLAP", pDEBUG) << "Input option: " << op << " was matched";
208 
209  if (strlen(&argv[2][0]) ) {
210  strcpy(argument,&argv[2][0]);
211  LOG("CLAP", pINFO)
212  << "Set opt = [" << op << "] to val = [" << argument << "]";
213 
214  } else if( (argc>2) &&
215  !(argv[2][0]=='-' &&argv[2][1]=='-' && isalpha(argv[2][2])) ) {
216  LOG("CLAP", pDEBUG)
217  << "argc>2 and next arg not a '--' followed by an alpha char";
218 
219  argc--;
220  argv++;
221  strcpy(argument,&argv[1][0]);
222  LOG("CLAP", pINFO)
223  << "Set opt = [" << op << "] to val = [" << argument << "]";
224  }
225  }
226  }
227  argc--;
228  argv++;
229  if(argc>2) {
230  LOG("CLAP", pDEBUG) << "Next argv[1][0] = " << argv[1][0];
231  }
232  }
233 
234  return argument;
235 }
236 //____________________________________________________________________________
238 {
239  const int buf_size = 2048*128;
240  bool set = false;
241 
242  int argc = fArgc;
243  char ** argv = fArgv;
244 
245  while(argc>1) {
246  if(argv[1][0] == '-' && argv[1][1] == '-') {
247  //char * op_cur = strndup(argv[1]+2,strlen(argv[1]));
248  char op_cur[buf_size];
249  strcpy(op_cur,&argv[1][2]);
250  if (strcmp(op.c_str(),op_cur)==0) set = true;
251  }
252  argc--;
253  argv++;
254  }
255 
256  return set;
257 }
258 //____________________________________________________________________________
260 {
261  char * argument = this->Arg(op);
262  string value = string(argument);
263  delete [] argument;
264 
265  return value;
266 }
267 //____________________________________________________________________________
269 {
270  char * argument = this->Arg(op);
271  double value = atof(argument);
272  delete [] argument;
273 
274  return value;
275 }
276 //____________________________________________________________________________
278 {
279  char * argument = this->Arg(op);
280  int value = atoi(argument);
281  delete [] argument;
282 
283  return value;
284 }
285 //____________________________________________________________________________
287 {
288  char * argument = this->Arg(op);
289  long value = atol(argument);
290  delete [] argument;
291 
292  return value;
293 }
294 //____________________________________________________________________________
long ArgAsLong(char opt)
double ArgAsDouble(char opt)
vector< string > ArgAsStringTokens(char opt, string delimeter)
string ArgAsString(char opt)
vector< long > ArgAsLongTokens(char opt, string delimeter)
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
vector< double > ArgAsDoubleTokens(char opt, string delimeter)
CmdLnArgParser(int argc, char **argv)
#define pINFO
Definition: Messenger.h:62
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:36
bool OptionExists(char opt)
was option set?
vector< int > ArgAsIntTokens(char opt, string delimeter)
#define pDEBUG
Definition: Messenger.h:63
char * Arg(char opt)
return argument following -`opt&#39;