41 CmdLine::CmdLine (
const int argc,
char** argv) {
42 __arguments.resize(argc);
43 for(
int iarg = 0; iarg < argc; iarg++){
44 __arguments[iarg] = argv[iarg];
50 CmdLine::CmdLine (
const vector<string> & args) {
56 void CmdLine::init (){
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;
89 bool CmdLine::present(
const string & opt)
const {
90 bool result = (__options.find(opt) != __options.end());
91 if (result) __options_used[opt] =
true;
96 bool CmdLine::present_and_set(
const string & opt)
const {
97 bool result = present(opt) && __options[opt] > 0;
103 string CmdLine::string_val(
const string & opt)
const {
104 if (!this->present_and_set(opt)) {
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;}
117 string CmdLine::string_val(
const string & opt,
const string & defval)
const {
118 if (this->present_and_set(opt)) {
return string_val(opt);}
119 else {
return defval;}
125 int CmdLine::int_val(
const string & opt)
const {
127 string optstring = string_val(opt);
128 istringstream optstream(optstring);
130 if (optstream.fail()) {
131 cerr <<
"Error: could not convert option ("<<opt<<
") value ("
132 <<optstring<<
") to int"<<endl;
138 int CmdLine::int_val(
const string & opt,
const int & defval)
const {
139 if (this->present_and_set(opt)) {
return int_val(opt);}
140 else {
return defval;}
147 double CmdLine::double_val(
const string & opt)
const {
149 string optstring = string_val(opt);
150 istringstream optstream(optstring);
152 if (optstream.fail()) {
153 cerr <<
"Error: could not convert option ("<<opt<<
") value ("
154 <<optstring<<
") to double"<<endl;
160 double CmdLine::double_val(
const string & opt,
const double & defval)
const {
161 if (this->present_and_set(opt)) {
return double_val(opt);}
162 else {
return defval;}
167 string CmdLine::command_line()
const {
168 return __command_line;
173 bool CmdLine::all_options_used()
const {
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;
186 void 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;