parse.hh (2041B)
1 #ifndef __BULKRENAME__PARSE__GUARD__ 2 #define __BULKRENAME__PARSE__GUARD__ 3 4 #include <string> 5 #include <vector> 6 #include <unordered_map> 7 #include <algorithm> 8 #include <iostream> 9 10 enum option_type_t { longopt, shortopt }; 11 12 struct option_t 13 { 14 option_t() = default; 15 16 option_t(option_type_t _type) 17 : type(_type), 18 has_value(false), 19 set(false) 20 {} 21 22 option_t(option_type_t _type, ::std::string _flag, 23 ::std::string _desc, bool _has_value = false) 24 : flag(_flag), 25 desc(_desc), 26 has_value(_has_value), 27 set(false) 28 {} 29 30 option_type_t type = shortopt; 31 ::std::string flag; 32 ::std::string desc; 33 ::std::string value; 34 bool has_value, set; 35 }; 36 37 38 class parser_t 39 { 40 public: 41 parser_t(int argc, char** argv) 42 : m_opts(argv, argv + argc), 43 m_optlist({ 44 // short options flag description 45 { option_type_t::shortopt, "n", "do not recurse into directories" }, 46 47 // long options flag description has value 48 { option_type_t::longopt, "no-recurse", "do not recurse into directories", false }, 49 }) 50 { 51 for (auto& opt : m_optlist) 52 m_optmap[opt.flag] = opt; 53 54 ::std::vector<::std::string>::iterator it; 55 if ((it = ::std::find(m_opts.begin(), m_opts.end(), "--")) != m_opts.end()) { 56 ::std::move(it + 1, m_opts.end(), ::std::back_inserter(m_args)); 57 m_opts.erase(it, m_opts.end()); 58 } 59 60 parse(); 61 } 62 63 void parse(); 64 void setopt(::std::string, ::std::string = ""); 65 bool isset(::std::string); 66 67 ::std::pair<int, ::std::vector<char*>> getargs(); 68 ::std::vector<option_t> getopts(); 69 70 private: 71 ::std::vector<::std::string> m_opts; 72 ::std::vector<::std::string> m_args; 73 74 ::std::vector<option_t> m_optlist; 75 ::std::unordered_map<::std::string, option_t> m_optmap; 76 77 }; 78 79 #endif//__BULKRENAME__PARSE__GUARD__