argparse.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. * \see http://stackoverflow.com/questions/865668/how-to-parse-command-line-arguments-in-c#868894
  33. */
  34. #ifndef argparse_hpp
  35. #define argparse_hpp
  36. #include <algorithm>
  37. #include <string>
  38. #include <vector>
  39. #include <regex>
  40. namespace fv_util {
  41. std::regex int_re("[0-9]+");
  42. std::regex float_re("([0-9]+\\.?[0-9]*)|([0-9]*\\.?[0-9]+)");
  43. class ArgParser {
  44. private:
  45. std::vector<std::string> tokens;
  46. public:
  47. ArgParser(int &argc, char **argv) {
  48. for (int i = 1; i < argc; ++i)
  49. this->tokens.push_back(std::string(argv[i]));
  50. }
  51. /// @author iain
  52. const std::string get_cmd_option(const std::string &option) const {
  53. std::vector<std::string>::const_iterator itr;
  54. itr = std::find(this->tokens.begin(), this->tokens.end(), option);
  55. if (itr != this->tokens.end() && ++itr != this->tokens.end()) {
  56. return *itr;
  57. }
  58. return "";
  59. }
  60. /// @author iain
  61. bool cmd_option_exists(const std::string &option) const {
  62. return std::find(this->tokens.begin(), this->tokens.end(), option)
  63. != this->tokens.end();
  64. }
  65. void update_config() {
  66. for (const std::string& s : tokens) {
  67. int split_pos = s.find("=");
  68. if (split_pos == std::string::npos) continue;
  69. std::string key = s.substr(0, split_pos);
  70. std::string val_raw = s.substr(split_pos+1, std::string::npos);
  71. std::smatch match;
  72. if(std::regex_match(val_raw, match, int_re)) {
  73. int val = atoi(val_raw.c_str());
  74. the_config->update_key(key, val);
  75. } else if (std::regex_match(val_raw, match, float_re)) {
  76. float val = atof(val_raw.c_str());
  77. the_config->update_key(key, val);
  78. } else {
  79. the_config->update_key(key, val_raw);
  80. }
  81. }
  82. }
  83. };
  84. }
  85. #endif // argparse_hpp