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