GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Private Attributes | List of all members
genie::CmdLnArgParser Class Reference

Command line argument parser. More...

#include <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< int > ArgAsIntTokens (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 <c.andreopoulos cern.ch> University of Liverpool
Created:
July 23, 2010
License:
Copyright (c) 2003-2024, The GENIE Collaboration For the full text of the license visit http://copyright.genie-mc.org

Definition at line 29 of file CmdLnArgParser.h.

Constructor & Destructor Documentation

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

Definition at line 22 of file CmdLnArgParser.cxx.

22  :
23 fArgc(argc),
24 fArgv(argv)
25 {
26 
27 }
CmdLnArgParser::~CmdLnArgParser ( )

Definition at line 29 of file CmdLnArgParser.cxx.

30 {
31 
32 }

Member Function Documentation

char * CmdLnArgParser::Arg ( char  opt)

return argument following -`opt'

Definition at line 34 of file CmdLnArgParser.cxx.

References fArgc, fArgv, LOG, pDEBUG, and pINFO.

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

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

return argument following –`opt'

Definition at line 186 of file CmdLnArgParser.cxx.

References fArgc, fArgv, LOG, pDEBUG, and pINFO.

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

Definition at line 123 of file CmdLnArgParser.cxx.

References Arg().

Referenced by GetCommandLineArgs(), and main().

124 {
125  char * argument = this->Arg(op);
126  double value = atof(argument);
127  delete [] argument;
128 
129  return value;
130 }
char * Arg(char opt)
return argument following -`opt&#39;
double CmdLnArgParser::ArgAsDouble ( string  opt)

Definition at line 268 of file CmdLnArgParser.cxx.

References Arg().

269 {
270  char * argument = this->Arg(op);
271  double value = atof(argument);
272  delete [] argument;
273 
274  return value;
275 }
char * Arg(char opt)
return argument following -`opt&#39;
vector< double > CmdLnArgParser::ArgAsDoubleTokens ( char  opt,
string  delimeter 
)

Definition at line 132 of file CmdLnArgParser.cxx.

References ArgAsStringTokens().

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

Definition at line 144 of file CmdLnArgParser.cxx.

References Arg().

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

145 {
146  char * argument = this->Arg(op);
147  int value = atoi(argument);
148  delete [] argument;
149 
150  return value;
151 }
char * Arg(char opt)
return argument following -`opt&#39;
int CmdLnArgParser::ArgAsInt ( string  opt)

Definition at line 277 of file CmdLnArgParser.cxx.

References Arg().

278 {
279  char * argument = this->Arg(op);
280  int value = atoi(argument);
281  delete [] argument;
282 
283  return value;
284 }
char * Arg(char opt)
return argument following -`opt&#39;
vector< int > CmdLnArgParser::ArgAsIntTokens ( char  opt,
string  delimeter 
)

Definition at line 153 of file CmdLnArgParser.cxx.

References ArgAsStringTokens().

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

Definition at line 165 of file CmdLnArgParser.cxx.

References Arg().

Referenced by GetCommandLineArgs(), and main().

166 {
167  char * argument = this->Arg(op);
168  long value = atol(argument);
169  delete [] argument;
170 
171  return value;
172 }
char * Arg(char opt)
return argument following -`opt&#39;
long CmdLnArgParser::ArgAsLong ( string  opt)

Definition at line 286 of file CmdLnArgParser.cxx.

References Arg().

287 {
288  char * argument = this->Arg(op);
289  long value = atol(argument);
290  delete [] argument;
291 
292  return value;
293 }
char * Arg(char opt)
return argument following -`opt&#39;
vector< long > CmdLnArgParser::ArgAsLongTokens ( char  opt,
string  delimeter 
)

Definition at line 174 of file CmdLnArgParser.cxx.

References ArgAsStringTokens().

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

Definition at line 106 of file CmdLnArgParser.cxx.

References Arg().

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

107 {
108  char * argument = this->Arg(op);
109  string value = string(argument);
110  delete [] argument;
111 
112  return value;
113 }
char * Arg(char opt)
return argument following -`opt&#39;
string CmdLnArgParser::ArgAsString ( string  opt)

Definition at line 259 of file CmdLnArgParser.cxx.

References Arg().

260 {
261  char * argument = this->Arg(op);
262  string value = string(argument);
263  delete [] argument;
264 
265  return value;
266 }
char * Arg(char opt)
return argument following -`opt&#39;
vector< string > CmdLnArgParser::ArgAsStringTokens ( char  opt,
string  delimeter 
)

Definition at line 115 of file CmdLnArgParser.cxx.

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

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

116 {
117  string argument = this->ArgAsString(op);
118 
119  vector<string> tokens = str::Split(argument, delimeter);
120  return tokens;
121 }
string ArgAsString(char opt)
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:36
bool CmdLnArgParser::OptionExists ( char  opt)

was option set?

Definition at line 88 of file CmdLnArgParser.cxx.

References fArgc, and fArgv.

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

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

was option set?

Definition at line 237 of file CmdLnArgParser.cxx.

References fArgc, and fArgv.

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 }

Member Data Documentation

int genie::CmdLnArgParser::fArgc
private

Definition at line 62 of file CmdLnArgParser.h.

Referenced by Arg(), and OptionExists().

char** genie::CmdLnArgParser::fArgv
private

Definition at line 63 of file CmdLnArgParser.h.

Referenced by Arg(), and OptionExists().


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