1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /**
- * @file
- * @author Caleb Fangmeier <caleb@fangmeier.tech>
- * @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 <algorithm>
- #include <string>
- #include <vector>
- #include <regex>
- 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<std::string> 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<std::string>::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
|