#pragma once #include #include #include #include using namespace std; class Value { private: string content; public: Value() : content("") {} Value(string content_) : content(content_) { } double asDouble(double def=0) const { if (content == "") return def; return (double) atof(content.c_str()); } int asInt(int def=0) const { if (content == "") return def; return atoi(content.c_str()); } bool asBool(bool def=false) const { if (content == "") return def; if (content == "0") return false; if (content == "false") return false; return true; } string asString(string def="") const { if (content == "") return def; return content; } }; class ArgumentParser { private: map mapArguments; const int iArgC; const char** vArgV; bool bStrictMode; public: Value operator()(const string arg) { if (bStrictMode) { map::const_iterator it = mapArguments.find(arg); if (it == mapArguments.end()) { printf("Runtime option NOT SPECIFIED! ABORTING! name: %s\n",arg.data()); abort(); } } return mapArguments[arg]; } ArgumentParser(const int argc, const char ** argv) : mapArguments(), iArgC(argc), vArgV(argv), bStrictMode(false) { for (int i=1; i::const_iterator it=mapArguments.begin(); it!=mapArguments.end(); it++) { options+= it->first + " " + it->second.asString() + " "; } string filepath = (path + "/" + string("argumentparser.log")); FILE * f = fopen(filepath.data(), "a"); if (f == NULL) { printf("impossible to write %s.\n", filepath.data()); return; } fprintf(f, "%s\n", options.data()); fclose(f); } };