Advertisement
Guest User

Untitled

a guest
May 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. #ifndef COMMAND_OPTION_H
  2. #define COMMAND_OPTION_H
  3.  
  4. #include <iostream>
  5. #include <map>
  6. #include <array>
  7. #include <vector>
  8. #include <string>
  9.  
  10.  
  11. /* command_option::extract(argc, argv, "Hello CommandOption") */
  12. /* ("v", v,"vector<int>の初期値") */
  13. /* ("d", d, "vector<double>の初期値") */
  14. /* ("s", s, "stringの初期値代入") */
  15. /* ("b", b, "boolの代入") */
  16. /* ("h", command_option::help, "ヘルプの表示"); */
  17.  
  18.  
  19.  
  20. struct command_option{
  21. private:
  22. using command_list = std::map<std::string, std::vector<std::string>>;
  23. using help_list = std::map<std::string, std::string>;
  24. using key_list = std::vector<std::string>;
  25. key_list keys;
  26. command_list m;
  27. help_list h;
  28.  
  29. struct command_parse{
  30. private:
  31. command_list& _map;
  32. help_list& _h;
  33. key_list& _keys;
  34. public:
  35. command_parse(command_list& m, help_list& h, key_list& keys)
  36. :_map(m), _h(h), _keys(keys){};
  37.  
  38. template <typename T>
  39. auto operator() (std::string cmd, std::string descript, T& t){
  40. if(_map.find(cmd) == _map.end())
  41. return *this;
  42. t = std::stod(_map[cmd][0]);
  43. _h[cmd] = descript;
  44. _keys.emplace_back(cmd);
  45. return *this;
  46. }
  47. template <>
  48. auto operator() (std::string cmd, std::string descript, bool& t){
  49. if(_map.find(cmd) == _map.end())
  50. return *this;
  51. if(_map[cmd][0] == "true")
  52. t = true;
  53. else if(_map[cmd][0] == "false")
  54. t = false;
  55. _h[cmd] = descript;
  56. _keys.emplace_back(cmd);
  57. return *this;
  58. }
  59. template <>
  60. auto operator() (std::string cmd, std::string descript, std::string& t){
  61. if(_map.find(cmd) == _map.end())
  62. return *this;
  63. t = _map[cmd][0];
  64. _h[cmd] = descript;
  65. _keys.emplace_back(cmd);
  66. return *this;
  67. }
  68.  
  69. template<typename U>
  70. auto operator() (std::string cmd, std::string descript, std::vector<U>& t){
  71. if(_map.find(cmd) == _map.end())
  72. return *this;
  73. _h[cmd] = descript;
  74. _keys.emplace_back(cmd);
  75. for(auto&&i : _map[cmd]){
  76. t.emplace_back(std::stod(i));
  77. }
  78. return *this;
  79. }
  80. template<>
  81. auto operator() (std::string cmd, std::string descript, std::vector<bool>& t){
  82. if(_map.find(cmd) == _map.end())
  83. return *this;
  84. _h[cmd] = descript;
  85. _keys.emplace_back(cmd);
  86. for(auto&&i : _map[cmd]){
  87. if(i == "true")
  88. t.emplace_back(true);
  89. if(i == "false")
  90. t.emplace_back(false);
  91. }
  92. return *this;
  93. }
  94. template<>
  95. auto operator() (std::string cmd, std::string descript, std::vector<std::string>& t){
  96. if(_map.find(cmd) == _map.end())
  97. return *this;
  98. _h[cmd] = descript;
  99. _keys.emplace_back(cmd);
  100. for(auto&&i : _map[cmd]){
  101. t.emplace_back(i);
  102. }
  103. return *this;
  104. }
  105.  
  106.  
  107. };
  108. public:
  109. auto extract(const int argc, const char** argv, std::string description){
  110. std::string map_key("");
  111. command_parse perser(m, h, keys);
  112. h[map_key] = description;
  113. for(int i = 1; i < argc; i++){
  114. if(argv[i][0] == '-'){
  115. map_key = std::string(&argv[i][1]);
  116. } else {
  117. m[map_key].emplace_back(std::string(argv[i]));
  118. }
  119. }
  120. return perser;
  121. }
  122. void help(){
  123.  
  124. std::cout << std::endl;
  125. std::cout << " " <<h[""] << std::endl;
  126. for(auto &&k : keys)
  127. std::cout << " " << k << "\t:" << h[k] <<std::endl;
  128. }
  129. };
  130.  
  131. #endif /* COMMAND_OPTION_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement