/** * @file * @author Caleb Fangmeier * @version 0.1 * * @section LICENSE * * * MIT License * * Copyright (c) 2017 Caleb Fangmeier * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * @section DESCRIPTION * \see http://stackoverflow.com/questions/865668/how-to-parse-command-line-arguments-in-c#868894 */ #ifndef argparse_hpp #define argparse_hpp #include #include #include #include namespace fv_util { std::regex int_re("[0-9]+"); std::regex float_re("([0-9]+\\.?[0-9]*)|([0-9]*\\.?[0-9]+)"); class ArgParser { private: std::vector tokens; public: ArgParser(int &argc, char **argv) { for (int i = 1; i < argc; ++i) this->tokens.push_back(std::string(argv[i])); } /// @author iain const std::string get_cmd_option(const std::string &option) const { std::vector::const_iterator itr; itr = std::find(this->tokens.begin(), this->tokens.end(), option); if (itr != this->tokens.end() && ++itr != this->tokens.end()) { return *itr; } return ""; } /// @author iain bool cmd_option_exists(const std::string &option) const { return std::find(this->tokens.begin(), this->tokens.end(), option) != this->tokens.end(); } void update_config() { for (const std::string& s : tokens) { int split_pos = s.find("="); if (split_pos == std::string::npos) continue; std::string key = s.substr(0, split_pos); std::string val_raw = s.substr(split_pos+1, std::string::npos); std::smatch match; if(std::regex_match(val_raw, match, int_re)) { int val = atoi(val_raw.c_str()); the_config->update_key(key, val); } else if (std::regex_match(val_raw, match, float_re)) { float val = atof(val_raw.c_str()); the_config->update_key(key, val); } else { the_config->update_key(key, val_raw); } } } }; } #endif // argparse_hpp