Public Member Functions | Private Attributes | List of all members
genie::CmdLnArgParser Class Reference

Command line argument parser. More...

#include "/cvmfs/nova.opensciencegrid.org/externals/genie/v3_00_06_p01/Linux64bit+2.6-2.12-e17-debug/GENIE-Generator/src/Framework/Utils/CmdLnArgParser.h"

Public Member Functions

 CmdLnArgParser (int argc, char **argv)
 
 ~CmdLnArgParser ()
 
bool OptionExists (char opt)
 was option set? More...
 
char * Arg (char opt)
 return argument following -`opt' More...
 
string ArgAsString (char opt)
 
vector< string > ArgAsStringTokens (char opt, string delimeter)
 
double ArgAsDouble (char opt)
 
vector< double > ArgAsDoubleTokens (char opt, string delimeter)
 
int ArgAsInt (char opt)
 
vector< intArgAsIntTokens (char opt, string delimeter)
 
long ArgAsLong (char opt)
 
vector< long > ArgAsLongTokens (char opt, string delimeter)
 
bool OptionExists (string opt)
 was option set? More...
 
char * Arg (string opt)
 return argument following –`opt' More...
 
string ArgAsString (string opt)
 
double ArgAsDouble (string opt)
 
int ArgAsInt (string opt)
 
long ArgAsLong (string opt)
 

Private Attributes

int fArgc
 
char ** fArgv
 

Detailed Description

Command line argument parser.

Author
Costas Andreopoulos <costas.andreopoulos stfc.ac.uk> University of Liverpool & STFC Rutherford Appleton Lab

July 23, 2010

Copyright (c) 2003-2019, The GENIE Collaboration For the full text of the license visit http://copyright.genie-mc.org or see $GENIE/LICENSE

Definition at line 30 of file CmdLnArgParser.h.

Constructor & Destructor Documentation

CmdLnArgParser::CmdLnArgParser ( int  argc,
char **  argv 
)

Definition at line 42 of file CmdLnArgParser.cxx.

42  :
43 fArgc(argc),
44 fArgv(argv)
45 {
46 
47 }
CmdLnArgParser::~CmdLnArgParser ( )

Definition at line 49 of file CmdLnArgParser.cxx.

50 {
51 
52 }

Member Function Documentation

char * CmdLnArgParser::Arg ( char  opt)

return argument following -`opt'

Definition at line 54 of file CmdLnArgParser.cxx.

References rootlogon::argv, fArgc, fArgv, LOG, pDEBUG, and pINFO.

Referenced by ArgAsDouble(), ArgAsInt(), ArgAsLong(), ArgAsString(), GetCommandLineArgs(), and main().

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 }
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:97
#define pINFO
Definition: Messenger.h:63
#define pDEBUG
Definition: Messenger.h:64
char * CmdLnArgParser::Arg ( string  opt)

return argument following –`opt'

Definition at line 206 of file CmdLnArgParser.cxx.

References rootlogon::argv, fArgc, fArgv, LOG, pDEBUG, and pINFO.

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 }
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:97
#define pINFO
Definition: Messenger.h:63
#define pDEBUG
Definition: Messenger.h:64
double CmdLnArgParser::ArgAsDouble ( char  opt)

Definition at line 143 of file CmdLnArgParser.cxx.

References Arg().

Referenced by GetCommandLineArgs(), and main().

144 {
145  char * argument = this->Arg(op);
146  double value = atof(argument);
147  delete [] argument;
148 
149  return value;
150 }
const XML_Char int const XML_Char * value
Definition: expat.h:331
char * Arg(char opt)
return argument following -`opt&#39;
double CmdLnArgParser::ArgAsDouble ( string  opt)

Definition at line 288 of file CmdLnArgParser.cxx.

References Arg().

289 {
290  char * argument = this->Arg(op);
291  double value = atof(argument);
292  delete [] argument;
293 
294  return value;
295 }
const XML_Char int const XML_Char * value
Definition: expat.h:331
char * Arg(char opt)
return argument following -`opt&#39;
vector< double > CmdLnArgParser::ArgAsDoubleTokens ( char  opt,
string  delimeter 
)

Definition at line 152 of file CmdLnArgParser.cxx.

References ArgAsStringTokens(), and parsePQSLOutput::tokens.

Referenced by GetCommandLineArgs().

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 }
vector< string > ArgAsStringTokens(char opt, string delimeter)
int CmdLnArgParser::ArgAsInt ( char  opt)

Definition at line 164 of file CmdLnArgParser.cxx.

References Arg().

Referenced by GetCommandLineArgs(), and genie::RunOpt::ReadFromCommandLine().

165 {
166  char * argument = this->Arg(op);
167  int value = atoi(argument);
168  delete [] argument;
169 
170  return value;
171 }
const XML_Char int const XML_Char * value
Definition: expat.h:331
char * Arg(char opt)
return argument following -`opt&#39;
int CmdLnArgParser::ArgAsInt ( string  opt)

Definition at line 297 of file CmdLnArgParser.cxx.

References Arg().

298 {
299  char * argument = this->Arg(op);
300  int value = atoi(argument);
301  delete [] argument;
302 
303  return value;
304 }
const XML_Char int const XML_Char * value
Definition: expat.h:331
char * Arg(char opt)
return argument following -`opt&#39;
vector< int > CmdLnArgParser::ArgAsIntTokens ( char  opt,
string  delimeter 
)

Definition at line 173 of file CmdLnArgParser.cxx.

References ArgAsStringTokens(), and parsePQSLOutput::tokens.

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 }
vector< string > ArgAsStringTokens(char opt, string delimeter)
long CmdLnArgParser::ArgAsLong ( char  opt)

Definition at line 185 of file CmdLnArgParser.cxx.

References Arg().

Referenced by GetCommandLineArgs(), and main().

186 {
187  char * argument = this->Arg(op);
188  long value = atol(argument);
189  delete [] argument;
190 
191  return value;
192 }
const XML_Char int const XML_Char * value
Definition: expat.h:331
char * Arg(char opt)
return argument following -`opt&#39;
long CmdLnArgParser::ArgAsLong ( string  opt)

Definition at line 306 of file CmdLnArgParser.cxx.

References Arg().

307 {
308  char * argument = this->Arg(op);
309  long value = atol(argument);
310  delete [] argument;
311 
312  return value;
313 }
const XML_Char int const XML_Char * value
Definition: expat.h:331
char * Arg(char opt)
return argument following -`opt&#39;
vector< long > CmdLnArgParser::ArgAsLongTokens ( char  opt,
string  delimeter 
)

Definition at line 194 of file CmdLnArgParser.cxx.

References ArgAsStringTokens(), and parsePQSLOutput::tokens.

Referenced by GetCommandLineArgs().

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 }
vector< string > ArgAsStringTokens(char opt, string delimeter)
string CmdLnArgParser::ArgAsString ( char  opt)

Definition at line 126 of file CmdLnArgParser.cxx.

References Arg().

Referenced by ArgAsStringTokens(), GetCommandLineArgs(), main(), and genie::RunOpt::ReadFromCommandLine().

127 {
128  char * argument = this->Arg(op);
129  string value = string(argument);
130  delete [] argument;
131 
132  return value;
133 }
const XML_Char int const XML_Char * value
Definition: expat.h:331
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
char * Arg(char opt)
return argument following -`opt&#39;
string CmdLnArgParser::ArgAsString ( string  opt)

Definition at line 279 of file CmdLnArgParser.cxx.

References Arg().

280 {
281  char * argument = this->Arg(op);
282  string value = string(argument);
283  delete [] argument;
284 
285  return value;
286 }
const XML_Char int const XML_Char * value
Definition: expat.h:331
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
char * Arg(char opt)
return argument following -`opt&#39;
vector< string > CmdLnArgParser::ArgAsStringTokens ( char  opt,
string  delimeter 
)

Definition at line 135 of file CmdLnArgParser.cxx.

References ArgAsString(), genie::utils::str::Split(), and parsePQSLOutput::tokens.

Referenced by ArgAsDoubleTokens(), ArgAsIntTokens(), ArgAsLongTokens(), and main().

136 {
137  string argument = this->ArgAsString(op);
138 
139  vector<string> tokens = str::Split(argument, delimeter);
140  return tokens;
141 }
string ArgAsString(char opt)
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:42
bool CmdLnArgParser::OptionExists ( char  opt)

was option set?

Definition at line 108 of file CmdLnArgParser.cxx.

References rootlogon::argv, fArgc, and fArgv.

Referenced by GetCommandLineArgs(), main(), and genie::RunOpt::ReadFromCommandLine().

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 }
bool CmdLnArgParser::OptionExists ( string  opt)

was option set?

Definition at line 257 of file CmdLnArgParser.cxx.

References rootlogon::argv, fArgc, and fArgv.

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 }

Member Data Documentation

int genie::CmdLnArgParser::fArgc
private

Definition at line 63 of file CmdLnArgParser.h.

Referenced by Arg(), and OptionExists().

char** genie::CmdLnArgParser::fArgv
private

Definition at line 64 of file CmdLnArgParser.h.

Referenced by Arg(), and OptionExists().


The documentation for this class was generated from the following files: