Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef COMMAND_OPTION_H
- #define COMMAND_OPTION_H
- #include <iostream>
- #include <map>
- #include <array>
- #include <vector>
- #include <string>
- /* command_option::extract(argc, argv, "Hello CommandOption") */
- /* ("v", v,"vector<int>の初期値") */
- /* ("d", d, "vector<double>の初期値") */
- /* ("s", s, "stringの初期値代入") */
- /* ("b", b, "boolの代入") */
- /* ("h", command_option::help, "ヘルプの表示"); */
- struct command_option{
- private:
- using command_list = std::map<std::string, std::vector<std::string>>;
- using help_list = std::map<std::string, std::string>;
- using key_list = std::vector<std::string>;
- key_list keys;
- command_list m;
- help_list h;
- struct command_parse{
- private:
- command_list& _map;
- help_list& _h;
- key_list& _keys;
- public:
- command_parse(command_list& m, help_list& h, key_list& keys)
- :_map(m), _h(h), _keys(keys){};
- template <typename T>
- auto operator() (std::string cmd, std::string descript, T& t){
- if(_map.find(cmd) == _map.end())
- return *this;
- t = std::stod(_map[cmd][0]);
- _h[cmd] = descript;
- _keys.emplace_back(cmd);
- return *this;
- }
- template <>
- auto operator() (std::string cmd, std::string descript, bool& t){
- if(_map.find(cmd) == _map.end())
- return *this;
- if(_map[cmd][0] == "true")
- t = true;
- else if(_map[cmd][0] == "false")
- t = false;
- _h[cmd] = descript;
- _keys.emplace_back(cmd);
- return *this;
- }
- template <>
- auto operator() (std::string cmd, std::string descript, std::string& t){
- if(_map.find(cmd) == _map.end())
- return *this;
- t = _map[cmd][0];
- _h[cmd] = descript;
- _keys.emplace_back(cmd);
- return *this;
- }
- template<typename U>
- auto operator() (std::string cmd, std::string descript, std::vector<U>& t){
- if(_map.find(cmd) == _map.end())
- return *this;
- _h[cmd] = descript;
- _keys.emplace_back(cmd);
- for(auto&&i : _map[cmd]){
- t.emplace_back(std::stod(i));
- }
- return *this;
- }
- template<>
- auto operator() (std::string cmd, std::string descript, std::vector<bool>& t){
- if(_map.find(cmd) == _map.end())
- return *this;
- _h[cmd] = descript;
- _keys.emplace_back(cmd);
- for(auto&&i : _map[cmd]){
- if(i == "true")
- t.emplace_back(true);
- if(i == "false")
- t.emplace_back(false);
- }
- return *this;
- }
- template<>
- auto operator() (std::string cmd, std::string descript, std::vector<std::string>& t){
- if(_map.find(cmd) == _map.end())
- return *this;
- _h[cmd] = descript;
- _keys.emplace_back(cmd);
- for(auto&&i : _map[cmd]){
- t.emplace_back(i);
- }
- return *this;
- }
- };
- public:
- auto extract(const int argc, const char** argv, std::string description){
- std::string map_key("");
- command_parse perser(m, h, keys);
- h[map_key] = description;
- for(int i = 1; i < argc; i++){
- if(argv[i][0] == '-'){
- map_key = std::string(&argv[i][1]);
- } else {
- m[map_key].emplace_back(std::string(argv[i]));
- }
- }
- return perser;
- }
- void help(){
- std::cout << std::endl;
- std::cout << " " <<h[""] << std::endl;
- for(auto &&k : keys)
- std::cout << " " << k << "\t:" << h[k] <<std::endl;
- }
- };
- #endif /* COMMAND_OPTION_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement