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 #ifndef __CMDLINE__
00027 #define __CMDLINE__
00028
00029 #include<string>
00030 #include<sstream>
00031 #include<map>
00032 #include<vector>
00033 using namespace std;
00034
00053 class CmdLine {
00054 mutable map<string,int> __options;
00055 vector<string> __arguments;
00056 mutable map<string,bool> __options_used;
00057
00058 string __command_line;
00059
00060 public :
00061 CmdLine() {};
00063 CmdLine(const int argc, char** argv);
00065 CmdLine(const vector<string> & args);
00066
00068 bool present(const string & opt) const;
00070 bool present_and_set(const string & opt) const;
00071
00074 inline const vector<string> & arguments() const {return __arguments;}
00075
00077 template<class T> T value(const string & opt) const;
00078 template<class T> T value(const string & opt, const T & defval) const;
00079
00080
00082 int int_val(const string & opt) const;
00084 int int_val(const string & opt, const int & defval) const;
00085
00087 double double_val(const string & opt) const;
00089 double double_val(const string & opt, const double & defval) const;
00090
00092 string string_val(const string & opt) const;
00094 string string_val(const string & opt, const string & defval) const;
00095
00097 string command_line() const;
00098
00100 bool all_options_used() const;
00101
00102 private:
00104 void init();
00105
00107 void _report_conversion_failure(const string & opt,
00108 const string & optstring) const;
00109
00110
00111 };
00112
00113
00114
00116 template<class T> T CmdLine::value(const string & opt) const {
00117 T result;
00118 string optstring = string_val(opt);
00119 istringstream optstream(optstring);
00120 optstream >> result;
00121 if (optstream.fail()) _report_conversion_failure(opt, optstring);
00122 return result;
00123 }
00124
00126 template<> inline string CmdLine::value<string>(const string & opt) const {
00127 return string_val(opt);}
00128
00129
00130
00131 template<class T> T CmdLine::value(const string & opt, const T & defval) const {
00132 if (this->present_and_set(opt)) {return value<T>(opt);}
00133 else {return defval;}
00134 }
00135
00136 #endif