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