41CmdLine::CmdLine (
const int argc,
char** argv) {
42 __arguments.resize(argc);
43 for(
int iarg = 0; iarg < argc; iarg++){
44 __arguments[iarg] = argv[iarg];
50CmdLine::CmdLine (
const vector<string> & args) {
58 for(
size_t iarg = 0; iarg < __arguments.size(); iarg++){
59 __command_line += __arguments[iarg];
60 __command_line +=
" ";
64 bool next_may_be_val =
false;
66 for(
size_t iarg = 1; iarg < __arguments.size(); iarg++){
69 if (next_may_be_val) {__options[currentopt] = iarg;}
71 string arg = __arguments[iarg];
72 bool thisisopt = (arg.compare(0,1,
"-") == 0);
77 __options[currentopt] = -1;
78 __options_used[currentopt] =
false;
79 next_may_be_val =
true;}
82 next_may_be_val =
false;
90 bool result = (__options.find(opt) != __options.end());
91 if (result) __options_used[opt] =
true;
97 bool result =
present(opt) && __options[opt] > 0;
105 cerr <<
"Error: Option "<<opt
106 <<
" is needed but is not present_and_set"<<endl;
109 string arg = __arguments[__options[opt]];
112 if (arg.compare(0,1,
"-") == 0) {__options_used[arg] =
true;}
119 else {
return defval;}
128 istringstream optstream(optstring);
130 if (optstream.fail()) {
131 cerr <<
"Error: could not convert option ("<<opt<<
") value ("
132 <<optstring<<
") to int"<<endl;
140 else {
return defval;}
150 istringstream optstream(optstring);
152 if (optstream.fail()) {
153 cerr <<
"Error: could not convert option ("<<opt<<
") value ("
154 <<optstring<<
") to double"<<endl;
162 else {
return defval;}
168 return __command_line;
175 for(map<string,bool>::const_iterator opt = __options_used.begin();
176 opt != __options_used.end(); opt++) {
177 bool this_one = opt->second;
178 if (! this_one) {cerr <<
"Option "<<opt->first<<
" unused/unrecognized"<<endl;}
179 result = result && this_one;
186void CmdLine::_report_conversion_failure(
const string & opt,
187 const string & optstring)
const {
188 cerr <<
"Error: could not convert option ("<<opt<<
") value ("
189 <<optstring<<
") to requested type"<<endl;
bool all_options_used() const
return true if all options have been asked for at some point or other
int int_val(const string &opt) const
return the integer value corresponding to the given option
double double_val(const string &opt) const
return the double value corresponding to the given option
bool present_and_set(const string &opt) const
true if the option is present and corresponds to a value
string command_line() const
return the full command line
string string_val(const string &opt) const
return the string value corresponding to the given option
bool present(const string &opt) const
true if the option is present