CmdLnArgParser.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2019, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5  or see $GENIE/LICENSE
6 
7  Author: Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk>
8  University of Liverpool & STFC Rutherford Appleton Lab
9 
10  For documentation see the corresponding header file.
11 
12  Important revisions after version 2.0.0 :
13  @ Jul 23, 2010 - CA
14  First added in v2.7.1. Adapted from previous code to parse cmd line args.
15  @ Feb 17, 2011 - JD
16  Fixed bug in ArgAsString so that it returns a NULL string if there is no
17  argument provided (as may be the case for a simple switch which can
18  optionally have an argument, like -S [filename] for save a file and if
19  supplied call it filename). Previously if no value was provided the there
20  was a chance that we return a ptr to an old bit of memory filled with the
21  argument from the last call to this function.
22  @ May 09, 2012 - CA
23  Add basic support for multi-character switches.
24  @ Jan 11, 2013 - CA
25  Added ArgAsString(), ArgAsDouble(), ArgAsInt(), ArgAsLong() for
26  multi-character switches
27 
28 */
29 //____________________________________________________________________________
30 
31 #include <cctype>
32 #include <cstdlib>
33 
37 
38 using namespace genie;
39 using namespace genie::utils;
40 
41 //____________________________________________________________________________
43 fArgc(argc),
44 fArgv(argv)
45 {
46 
47 }
48 //____________________________________________________________________________
50 {
51 
52 }
53 //____________________________________________________________________________
54 char * CmdLnArgParser::Arg(char op)
55 {
56  const int buf_size = 2048*128;
57 
58  bool set = false;
59  char * argument = new char[buf_size];
60  strcpy(argument, "");
61 
62  int argc = fArgc;
63  char ** argv = fArgv;
64 
65  while(argc>2)
66  {
67  LOG("CLAP", pDEBUG) << "Getting next argument in argument-list";
68  LOG("CLAP", pDEBUG) << "Current argc = " << argc;
69 
70  if (argv[1][0] == '-') {
71  LOG("CLAP", pDEBUG)
72  << "Got char (argv[1][1]) following argv[1][0]='-' : " << argv[1][1];
73 
74  if (argv[1][1] == op) {
75  LOG("CLAP", pDEBUG) << "Input option: " << op << " was matched";
76 
77  if (strlen(&argv[1][2]) ) {
78  strcpy(argument,&argv[1][2]);
79  LOG("CLAP", pINFO)
80  << "Set opt = [" << op << "] to val = [" << argument << "]";
81 
82  } else if( (argc>2) &&
83  !(argv[2][0]=='-' && isalpha(argv[2][1])) ) {
84 
85  LOG("CLAP", pDEBUG)
86  << "argc>2 and next arg not a '-' followed by an alpha char";
87 
88  argc--;
89  argv++;
90  strcpy(argument,&argv[1][0]);
91  set = true;
92  LOG("CLAP", pINFO)
93  << "Set opt = [" << op << "] to val = [" << argument << "]";
94  }
95  }
96  }
97  argc--;
98  argv++;
99  if(argc>2) {
100  LOG("CLAP", pDEBUG) << "Next argv[1][0] = " << argv[1][0];
101  }
102  }
103 
104  LOG("CLAP", pDEBUG) << "CmdLnArgParser::Arg op='" << op << "' set=" << set;
105  return argument;
106 }
107 //____________________________________________________________________________
109 {
110  bool set = false;
111 
112  int argc = fArgc;
113  char ** argv = fArgv;
114 
115  while(argc>1) {
116  if(argv[1][0] == '-') {
117  if (argv[1][1] == op) set = true;
118  }
119  argc--;
120  argv++;
121  }
122 
123  return set;
124 }
125 //____________________________________________________________________________
127 {
128  char * argument = this->Arg(op);
129  string value = string(argument);
130  delete [] argument;
131 
132  return value;
133 }
134 //____________________________________________________________________________
135 vector<string> CmdLnArgParser::ArgAsStringTokens(char op, string delimeter)
136 {
137  string argument = this->ArgAsString(op);
138 
139  vector<string> tokens = str::Split(argument, delimeter);
140  return tokens;
141 }
142 //____________________________________________________________________________
144 {
145  char * argument = this->Arg(op);
146  double value = atof(argument);
147  delete [] argument;
148 
149  return value;
150 }
151 //____________________________________________________________________________
152 vector<double> CmdLnArgParser::ArgAsDoubleTokens(char op, string delimeter)
153 {
154  vector<string> strtokens = this->ArgAsStringTokens(op, delimeter);
155  vector<double> tokens;
156  vector<string>::const_iterator iter = strtokens.begin();
157  for( ; iter != strtokens.end(); ++iter) {
158  string arg = *iter;
159  tokens.push_back(atof(arg.c_str()));
160  }
161  return tokens;
162 }
163 //____________________________________________________________________________
165 {
166  char * argument = this->Arg(op);
167  int value = atoi(argument);
168  delete [] argument;
169 
170  return value;
171 }
172 //____________________________________________________________________________
173 vector<int> CmdLnArgParser::ArgAsIntTokens(char op, string delimeter)
174 {
175  vector<string> strtokens = this->ArgAsStringTokens(op, delimeter);
176  vector<int> tokens;
177  vector<string>::const_iterator iter = strtokens.begin();
178  for( ; iter != strtokens.end(); ++iter) {
179  string arg = *iter;
180  tokens.push_back(atoi(arg.c_str()));
181  }
182  return tokens;
183 }
184 //____________________________________________________________________________
186 {
187  char * argument = this->Arg(op);
188  long value = atol(argument);
189  delete [] argument;
190 
191  return value;
192 }
193 //____________________________________________________________________________
194 vector<long> CmdLnArgParser::ArgAsLongTokens(char op, string delimeter)
195 {
196  vector<string> strtokens = this->ArgAsStringTokens(op, delimeter);
197  vector<long> tokens;
198  vector<string>::const_iterator iter = strtokens.begin();
199  for( ; iter != strtokens.end(); ++iter) {
200  string arg = *iter;
201  tokens.push_back(atol(arg.c_str()));
202  }
203  return tokens;
204 }
205 //____________________________________________________________________________
206 char * CmdLnArgParser::Arg(string op)
207 {
208  const int buf_size = 2048*128;
209  char * argument = new char[buf_size];
210  strcpy(argument, "");
211 
212  int argc = fArgc;
213  char ** argv = fArgv;
214 
215  while(argc>2)
216  {
217  LOG("CLAP", pDEBUG) << "Getting next argument in argument-list";
218  LOG("CLAP", pDEBUG) << "Current argc = " << argc;
219 
220  if (argv[1][0] == '-' && argv[1][1] == '-') {
221  //char * op_cur = strndup(argv[1]+2,strlen(argv[1]));
222  char op_cur[buf_size];
223  strcpy(op_cur,&argv[1][2]);
224  LOG("CLAP", pDEBUG)
225  << "Got string following '--' : " << op_cur;
226  if (strcmp(op.c_str(),op_cur)==0) {
227  LOG("CLAP", pDEBUG) << "Input option: " << op << " was matched";
228 
229  if (strlen(&argv[2][0]) ) {
230  strcpy(argument,&argv[2][0]);
231  LOG("CLAP", pINFO)
232  << "Set opt = [" << op << "] to val = [" << argument << "]";
233 
234  } else if( (argc>2) &&
235  !(argv[2][0]=='-' &&argv[2][1]=='-' && isalpha(argv[2][2])) ) {
236  LOG("CLAP", pDEBUG)
237  << "argc>2 and next arg not a '--' followed by an alpha char";
238 
239  argc--;
240  argv++;
241  strcpy(argument,&argv[1][0]);
242  LOG("CLAP", pINFO)
243  << "Set opt = [" << op << "] to val = [" << argument << "]";
244  }
245  }
246  }
247  argc--;
248  argv++;
249  if(argc>2) {
250  LOG("CLAP", pDEBUG) << "Next argv[1][0] = " << argv[1][0];
251  }
252  }
253 
254  return argument;
255 }
256 //____________________________________________________________________________
258 {
259  const int buf_size = 2048*128;
260  bool set = false;
261 
262  int argc = fArgc;
263  char ** argv = fArgv;
264 
265  while(argc>1) {
266  if(argv[1][0] == '-' && argv[1][1] == '-') {
267  //char * op_cur = strndup(argv[1]+2,strlen(argv[1]));
268  char op_cur[buf_size];
269  strcpy(op_cur,&argv[1][2]);
270  if (strcmp(op.c_str(),op_cur)==0) set = true;
271  }
272  argc--;
273  argv++;
274  }
275 
276  return set;
277 }
278 //____________________________________________________________________________
280 {
281  char * argument = this->Arg(op);
282  string value = string(argument);
283  delete [] argument;
284 
285  return value;
286 }
287 //____________________________________________________________________________
289 {
290  char * argument = this->Arg(op);
291  double value = atof(argument);
292  delete [] argument;
293 
294  return value;
295 }
296 //____________________________________________________________________________
298 {
299  char * argument = this->Arg(op);
300  int value = atoi(argument);
301  delete [] argument;
302 
303  return value;
304 }
305 //____________________________________________________________________________
307 {
308  char * argument = this->Arg(op);
309  long value = atol(argument);
310  delete [] argument;
311 
312  return value;
313 }
314 //____________________________________________________________________________
long ArgAsLong(char opt)
double ArgAsDouble(char opt)
vector< string > ArgAsStringTokens(char opt, string delimeter)
string ArgAsString(char opt)
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
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:97
const XML_Char int const XML_Char * value
Definition: expat.h:331
vector< double > ArgAsDoubleTokens(char opt, string delimeter)
CmdLnArgParser(int argc, char **argv)
#define pINFO
Definition: Messenger.h:63
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:42
bool OptionExists(char opt)
was option set?
vector< int > ArgAsIntTokens(char opt, string delimeter)
Root of GENIE utility namespaces.
#define pDEBUG
Definition: Messenger.h:64
char * Arg(char opt)
return argument following -`opt&#39;
enum BeamMode string