17 template<
typename argType>
26 : mName(name), mDesc(descrip), mHasDefault(false), mHasValue(false) {}
29 : mName(name), mDesc(descrip), mValue(T), mHasDefault(true), mHasValue(true) {}
31 void SetValue(argType &T) { mValue = T; mHasValue=
true; }
33 void SetDefault(argType &T) { mValue = T; mHasDefault =
true; }
43 friend bool operator< (const commandArg<argType> &lhs,
46 if ( lhs.GetName() < rhs.GetName() )
54 string type =
typeid(*this).name();
55 if ( type.find(
"Ib") != string::npos )
57 else if ( type.find(
"Ii") != string::npos )
59 else if ( type.find(
"Id") != string::npos )
61 else if ( type.find(
"ISs") != string::npos )
64 return "unknown type";
83 : mArgc(argc), mArgv(argv), mDesc(description), mNumDefaults(0),mNumArgs(0) {}
104 if ( mHelp.str().empty() )
116 mHelp << o.str() <<
")";
119 mHelp <<
"\n" << ends;
121 mArgNames.insert(arg.
GetName());
129 if ( mHelp.str().empty() )
141 mHelp << o.str() <<
")";
144 mHelp <<
"\n" << ends;
146 mCArgNames.insert(arg.
GetName());
153 if ( mArgc == 1 && mNumArgs != mNumDefaults)
165 if ( mArgNames.empty() )
181 v=string(mArgv[i+1]);
183 bool is_compound(
false);
184 set<string>::iterator sIter = mArgNames.find(n);
185 if ( sIter == mArgNames.end() )
187 sIter = mCArgNames.find(n);
188 if (sIter==mCArgNames.end())
190 if (n ==
"-print-command-line") {
191 cout <<
"----------------------- Welcome to Spines -------------------------------" << endl;
192 cout <<
"This module was invoked via:" << endl;
193 for (
int k=0; k<mArgc; k++) {
194 cout << mArgv[k] <<
" ";
197 cout <<
"----------------------- Welcome to Spines -------------------------------" << endl << endl;
206 cout <<
"\nInvalid command-line arg: " << n << endl;
223 bool is_float(
false);
224 if (num_v>1) is_float = sp.
IsFloat(1);
228 mNameVal.insert(make_pair(n,v));
233 mNameVal.insert(make_pair(n,v));
239 mNameVal.insert(make_pair(n,v));
249 for (
int k=0; k<num_v; ++k)
250 mNameVal.insert(make_pair(n,sp.
AsString(k)));
261 map<string,string>::iterator mIter = mNameVal.find(key);
262 if ( mIter == mNameVal.end() )
267 if ( keyArg.
GetType() ==
"string" )
271 cout <<
"need to specify " << key << endl;
279 return (mIter->second);
286 vector<string> dummy;
289 pair<multimap<string,string>::iterator,multimap<string,string>::iterator> mIter = mNameVal.equal_range(key);
290 if ( mIter.first == mIter.second)
295 if ( keyArg.
GetType() ==
"string" )
299 cout <<
"need to specify " << key << endl;
303 dummy.push_back(o.str());
309 for (; mIter.first != mIter.second; ++mIter.first)
310 dummy.push_back(mIter.first->second);
320 string val = GetStringValueFor(keyArg);
326 cout <<
"need to specify " << key << endl;
340 return (atof(val.c_str()));
348 string val = GetStringValueFor(keyArg);
353 cout <<
"need to specify " << key << endl;
367 return (atoi(val.c_str()));
375 map<string,string>::iterator mIter = mNameVal.find(key);
376 if ( mIter == mNameVal.end() )
384 cout <<
"need to specify " << key << endl;
389 if (mIter->second ==
"0" || mIter->second ==
"false")
399 for (
int i=1; i<mArgc; ++i )
401 string this_arg(mArgv[i]);
402 if ( this_arg ==
"-h" )
411 cout << endl << mArgv[0] <<
": ";
413 cout << mDesc << endl << endl;
415 cout <<
"a module in the code base 'Spines'." << endl << endl;
416 cout <<
"\nAvailable arguments:" << endl;
417 cout << mHelp.str() << endl;
bool ContainsAt(string &s, string &t, int at)
Definition: StringUtil.cc:82
bool HasValue()
Definition: CommandLineParser.h:37
bool IsFloat(int index)
Definition: FileParser.cc:71
bool GetBoolValueFor(commandArg< bool > &keyArg)
Definition: CommandLineParser.h:372
string GetName()
Definition: CommandLineParser.h:40
int mArgc
Definition: CommandLineParser.h:90
void SetValue(argType &T)
Definition: CommandLineParser.h:31
commandArg(string name, string descrip, argType T)
Definition: CommandLineParser.h:28
commandLineParser(int argc, char **argv, const string &description="")
Definition: CommandLineParser.h:82
stringstream mHelp
Definition: CommandLineParser.h:92
int GetItemCount()
Definition: FileParser.cc:45
argType GetValue()
Definition: CommandLineParser.h:39
bool mHasValue
Definition: CommandLineParser.h:72
int GetIntValueFor(commandArg< int > &keyArg)
Definition: CommandLineParser.h:345
void registerCompoundArg(commandArg< T > &arg)
Definition: CommandLineParser.h:127
void showHelp()
Definition: CommandLineParser.h:409
vector< string > GetCompoundStringValuesFor(commandArg< T > &keyArg)
Definition: CommandLineParser.h:284
multimap< string, string > mNameVal
Definition: CommandLineParser.h:96
commandArg()
Definition: CommandLineParser.h:23
void SetDescription(string d)
Definition: CommandLineParser.h:34
string GetStringValueFor(commandArg< T > &keyArg)
Definition: CommandLineParser.h:258
string mName
Definition: CommandLineParser.h:70
bool HasDefault()
Definition: CommandLineParser.h:36
bool parse()
Definition: CommandLineParser.h:151
int mNumDefaults
Definition: CommandLineParser.h:95
void registerArg(commandArg< T > &arg)
Definition: CommandLineParser.h:102
string mDesc
Definition: CommandLineParser.h:93
bool requestHelp()
Definition: CommandLineParser.h:397
set< string > mCArgNames
Definition: CommandLineParser.h:97
Definition: FileParser.h:17
void SetDefault(argType &T)
Definition: CommandLineParser.h:33
void SetLine(const string &line)
Definition: FileParser.cc:33
argType mValue
Definition: CommandLineParser.h:71
Definition: CommandLineParser.h:77
const string & AsString(int index)
Definition: FileParser.cc:87
string GetType()
Definition: CommandLineParser.h:52
string GetDescription()
Definition: CommandLineParser.h:41
double GetDoubleValueFor(commandArg< double > &keyArg)
Definition: CommandLineParser.h:317
char ** mArgv
Definition: CommandLineParser.h:91
commandArg(string name, string descrip)
Definition: CommandLineParser.h:25
Definition: CommandLineParser.h:18
void SetDescription(const string &s)
Definition: CommandLineParser.h:85