00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00025
00026
00027 #include "CmdLine.hh"
00028 #include<string>
00029 #include<sstream>
00030 #include<iostream>
00031 #include<vector>
00032 #include<cstddef>
00033 using namespace std;
00034
00035
00036
00037
00038
00039
00040 CmdLine::CmdLine (const int argc, char** argv) {
00041 __arguments.resize(argc);
00042 for(int iarg = 0; iarg < argc; iarg++){
00043 __arguments[iarg] = argv[iarg];
00044 }
00045 this->init();
00046 }
00047
00049 CmdLine::CmdLine (const vector<string> & args) {
00050 __arguments = args;
00051 this->init();
00052 }
00053
00054
00055 void CmdLine::init (){
00056 __command_line = "";
00057 for(size_t iarg = 0; iarg < __arguments.size(); iarg++){
00058 __command_line += __arguments[iarg];
00059 __command_line += " ";
00060 }
00061
00062
00063 bool next_may_be_val = false;
00064 string currentopt;
00065 for(size_t iarg = 1; iarg < __arguments.size(); iarg++){
00066
00067
00068 if (next_may_be_val) {__options[currentopt] = iarg;}
00069
00070 string arg = __arguments[iarg];
00071 bool thisisopt = (arg.compare(0,1,"-") == 0);
00072 if (thisisopt) {
00073
00074
00075 currentopt = arg;
00076 __options[currentopt] = -1;
00077 __options_used[currentopt] = false;
00078 next_may_be_val = true;}
00079 else {
00080
00081 next_may_be_val = false;
00082 currentopt = "";
00083 }
00084 }
00085 }
00086
00087
00088 bool CmdLine::present(const string & opt) const {
00089 bool result = (__options.find(opt) != __options.end());
00090 if (result) __options_used[opt] = true;
00091 return result;
00092 }
00093
00094
00095 bool CmdLine::present_and_set(const string & opt) const {
00096 bool result = present(opt) && __options[opt] > 0;
00097 return result;
00098 }
00099
00100
00101
00102 string CmdLine::string_val(const string & opt) const {
00103 if (!this->present_and_set(opt)) {
00104 cerr << "Error: Option "<<opt
00105 <<" is needed but is not present_and_set"<<endl;
00106 exit(-1);
00107 }
00108 string arg = __arguments[__options[opt]];
00109
00110
00111 if (arg.compare(0,1,"-") == 0) {__options_used[arg] = true;}
00112 return arg;
00113 }
00114
00115
00116 string CmdLine::string_val(const string & opt, const string & defval) const {
00117 if (this->present_and_set(opt)) {return string_val(opt);}
00118 else {return defval;}
00119 }
00120
00121
00122
00123
00124 int CmdLine::int_val(const string & opt) {
00125 int result;
00126 string optstring = string_val(opt);
00127 istringstream optstream(optstring);
00128 optstream >> result;
00129 if (optstream.fail()) {
00130 cerr << "Error: could not convert option ("<<opt<<") value ("
00131 <<optstring<<") to int"<<endl;
00132 exit(-1);}
00133 return result;
00134 }
00135
00136
00137 int CmdLine::int_val(const string & opt, const int & defval) {
00138 if (this->present_and_set(opt)) {return int_val(opt);}
00139 else {return defval;}
00140 }
00141
00142
00143
00144
00145
00146 double CmdLine::double_val(const string & opt) const {
00147 double result;
00148 string optstring = string_val(opt);
00149 istringstream optstream(optstring);
00150 optstream >> result;
00151 if (optstream.fail()) {
00152 cerr << "Error: could not convert option ("<<opt<<") value ("
00153 <<optstring<<") to double"<<endl;
00154 exit(-1);}
00155 return result;
00156 }
00157
00158
00159 double CmdLine::double_val(const string & opt, const double & defval) const {
00160 if (this->present_and_set(opt)) {return double_val(opt);}
00161 else {return defval;}
00162 }
00163
00164
00165
00166 string CmdLine::command_line() const {
00167 return __command_line;
00168 }
00169
00170
00171
00172 bool CmdLine::all_options_used() const {
00173 bool result = true;
00174 for(map<string,bool>::const_iterator opt = __options_used.begin();
00175 opt != __options_used.end(); opt++) {
00176 bool this_one = opt->second;
00177 if (! this_one) {cerr << "Option "<<opt->first<<" unused/unrecognized"<<endl;}
00178 result = result && this_one;
00179 }
00180 return result;
00181 }
00182
00183
00185 void CmdLine::_report_conversion_failure(const string & opt,
00186 const string & optstring) const {
00187 cerr << "Error: could not convert option ("<<opt<<") value ("
00188 <<optstring<<") to requested type"<<endl;
00189 exit(-1);
00190 }
00191