#ifndef STIM_ARGUMENTS #define STIM_ARGUMENTS #include #include #include #include #include #include #include #ifdef _WIN32 #include #endif namespace stim{ class cmd_option { private: bool ansi; //argument name std::string name; //description of the argument std::vector desc; //argument values std::vector vals; //range or example std::string range; //flag is true when the argument is user-specified bool flag; void parse_val(const std::string &s){ vals.clear(); std::stringstream ss(s); std::string item; while (std::getline(ss, item, ' ')) { vals.push_back(item); } } void parse_desc(const std::string &s){ desc.clear(); std::stringstream ss(s); std::string item; while (std::getline(ss, item, '\n')) { desc.push_back(item); } } public: void set_ansi(bool b){ ansi = b; } //create an option with a given name, description, and default value cmd_option(std::string _name, std::string _desc, std::string _default = "", std::string _range = "") { name = _name; parse_desc(_desc); parse_val(_default); //if a default value is provided, set the flag if(_default != "") flag = true; else flag = false; range = _range; } int nargs() { return vals.size(); } //return the value of a text option std::string as_string(unsigned int n = 0) { if(!flag) { std::cout<<"ERROR - Option requested without being set: "< n) return vals[n]; else return ""; } //return the value of a floating point option float as_float(unsigned int n = 0) { if(!flag) { std::cout<<"ERROR - option requested without being set: "< n) { float r; if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; return r; } else return 0; } //return the value of an integer option int as_int(unsigned int n = 0) { if(!flag) { std::cout<<"ERROR - option requested without being set: "< n) { int r; if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; return r; } else return 0; } //get the width of the left column int col_width() { int n = 3; //add the length of the option name n += name.size(); //if there are any default parameters if(vals.size() > 0) { //padding (parenthesis, =, etc.) n += 6; //for each default option value for(unsigned int v=0; v opts; std::vector args; //column width of the longest option int col_width; //list of sections std::vector sections; public: arglist(){ col_width = 0; ansi = true; //set ansi to false by default if this is a Windows system // (console doesn't support ANSI colors) #ifdef _WIN32 ansi=false; #endif } ///Sets ANSI support (default is on), which allows colored values in the help output. /// @param b is a boolean value specifying ANSI support (true is on, false is off) void set_ansi(bool b) { ansi = b; for(unsigned int i=0; i(col_width, opt.col_width()); } ///Specifies a section name that can be used to organize parameters in the output. /// @param _name is the name of the section, which will be displayed to the user void section(std::string _name) { argsection s; s.name = _name; s.index = opts.size(); sections.push_back(s); } ///Outputs supported arguments. If ANSI support is available, they will be color-coded. Generally this is called in response to "--help" std::string str() { std::stringstream ss; int si = -1; if(sections.size() > 0) si = 0; //for each option for(unsigned int a=0; a= opts.size()) return -1; return (int)i; } ///Sets an argument to a specified value /// @param _name is the name of the argument to be set /// @param _value is the value that it is given void set(std::string _name, std::string _value) { int i = index(_name); if(i != -1) { opts[i].set(_value); //adjust the column width if necessary col_width = (std::max)(col_width, opts[i].col_width()); } else std::cout<<"ERROR - option not recognized: "<<_name<= opts.size()) { std::cout<<"ERROR - Unspecified parameter name: "<<_name<