GENIEGenerator
Loading...
Searching...
No Matches
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?
char * Arg (char opt)
 return argument following -‘opt’
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?
char * Arg (string opt)
 return argument following –‘opt’
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 \at cern.ch> University of Liverpool
Created:\n July 23, 2010
License:\n Copyright (c) 2003-2025, 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::CmdLnArgParser ( int argc,
char ** argv )

Definition at line 22 of file CmdLnArgParser.cxx.

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

References fArgc, and fArgv.

◆ ~CmdLnArgParser()

CmdLnArgParser::~CmdLnArgParser ( )

Definition at line 29 of file CmdLnArgParser.cxx.

30{
31
32}

Member Function Documentation

◆ Arg() [1/2]

char * CmdLnArgParser::Arg ( char opt)

return argument following -‘opt’

Definition at line 34 of file CmdLnArgParser.cxx.

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

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

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

◆ Arg() [2/2]

char * CmdLnArgParser::Arg ( string opt)

return argument following –‘opt’

Definition at line 186 of file CmdLnArgParser.cxx.

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}

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

◆ ArgAsDouble() [1/2]

double CmdLnArgParser::ArgAsDouble ( char opt)

Definition at line 123 of file CmdLnArgParser.cxx.

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’

References Arg().

Referenced by GetCommandLineArgs(), and main().

◆ ArgAsDouble() [2/2]

double CmdLnArgParser::ArgAsDouble ( string opt)

Definition at line 268 of file CmdLnArgParser.cxx.

269{
270 char * argument = this->Arg(op);
271 double value = atof(argument);
272 delete [] argument;
273
274 return value;
275}

References Arg().

◆ ArgAsDoubleTokens()

vector< double > CmdLnArgParser::ArgAsDoubleTokens ( char opt,
string delimeter )

Definition at line 132 of file CmdLnArgParser.cxx.

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)

References ArgAsStringTokens().

Referenced by GetCommandLineArgs().

◆ ArgAsInt() [1/2]

int CmdLnArgParser::ArgAsInt ( char opt)

Definition at line 144 of file CmdLnArgParser.cxx.

145{
146 char * argument = this->Arg(op);
147 int value = atoi(argument);
148 delete [] argument;
149
150 return value;
151}

References Arg().

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

◆ ArgAsInt() [2/2]

int CmdLnArgParser::ArgAsInt ( string opt)

Definition at line 277 of file CmdLnArgParser.cxx.

278{
279 char * argument = this->Arg(op);
280 int value = atoi(argument);
281 delete [] argument;
282
283 return value;
284}

References Arg().

◆ ArgAsIntTokens()

vector< int > CmdLnArgParser::ArgAsIntTokens ( char opt,
string delimeter )

Definition at line 153 of file CmdLnArgParser.cxx.

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}

References ArgAsStringTokens().

◆ ArgAsLong() [1/2]

long CmdLnArgParser::ArgAsLong ( char opt)

Definition at line 165 of file CmdLnArgParser.cxx.

166{
167 char * argument = this->Arg(op);
168 long value = atol(argument);
169 delete [] argument;
170
171 return value;
172}

References Arg().

Referenced by GetCommandLineArgs(), and main().

◆ ArgAsLong() [2/2]

long CmdLnArgParser::ArgAsLong ( string opt)

Definition at line 286 of file CmdLnArgParser.cxx.

287{
288 char * argument = this->Arg(op);
289 long value = atol(argument);
290 delete [] argument;
291
292 return value;
293}

References Arg().

◆ ArgAsLongTokens()

vector< long > CmdLnArgParser::ArgAsLongTokens ( char opt,
string delimeter )

Definition at line 174 of file CmdLnArgParser.cxx.

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}

References ArgAsStringTokens().

Referenced by GetCommandLineArgs().

◆ ArgAsString() [1/2]

string CmdLnArgParser::ArgAsString ( char opt)

Definition at line 106 of file CmdLnArgParser.cxx.

107{
108 char * argument = this->Arg(op);
109 string value = string(argument);
110 delete [] argument;
111
112 return value;
113}

References Arg().

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

◆ ArgAsString() [2/2]

string CmdLnArgParser::ArgAsString ( string opt)

Definition at line 259 of file CmdLnArgParser.cxx.

260{
261 char * argument = this->Arg(op);
262 string value = string(argument);
263 delete [] argument;
264
265 return value;
266}

References Arg().

◆ ArgAsStringTokens()

vector< string > CmdLnArgParser::ArgAsStringTokens ( char opt,
string delimeter )

Definition at line 115 of file CmdLnArgParser.cxx.

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)

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

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

◆ OptionExists() [1/2]

bool CmdLnArgParser::OptionExists ( char opt)

was option set?

Definition at line 88 of file CmdLnArgParser.cxx.

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}

References fArgc, and fArgv.

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

◆ OptionExists() [2/2]

bool CmdLnArgParser::OptionExists ( string opt)

was option set?

Definition at line 237 of file CmdLnArgParser.cxx.

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}

References fArgc, and fArgv.

Member Data Documentation

◆ fArgc

int genie::CmdLnArgParser::fArgc
private

Definition at line 62 of file CmdLnArgParser.h.

Referenced by Arg(), Arg(), CmdLnArgParser(), OptionExists(), and OptionExists().

◆ fArgv

char** genie::CmdLnArgParser::fArgv
private

Definition at line 63 of file CmdLnArgParser.h.

Referenced by Arg(), Arg(), CmdLnArgParser(), OptionExists(), and OptionExists().


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