parse.cc (1497B)
1 #include "parse.hh" 2 #include "util.hh" 3 4 5 void 6 parser_t::parse() 7 { 8 for (size_t i = 0; i < m_opts.size(); ++i) { 9 if (m_opts[i].rfind("--", 0) != ::std::string::npos) { 10 ::std::string optflag = m_opts[i].substr(2); 11 if (!m_optmap.count(optflag)) continue; 12 option_t opt = m_optmap[optflag]; 13 if (opt.has_value && i+1 < m_opts.size()) { 14 setopt(optflag, m_opts[i+1]); 15 ++i; 16 } else if (!opt.flag.empty() && !opt.has_value) 17 setopt(optflag); 18 } else if (m_opts[i].rfind("-", 0) != ::std::string::npos) { 19 ::std::string optflags = m_opts[i].substr(1); 20 for (auto& optflag : optflags) 21 setopt(::std::string(1, optflag)); 22 } else if (i) { 23 m_args.push_back(m_opts[i]); 24 } 25 } 26 } 27 28 void 29 parser_t::setopt(::std::string flag, ::std::string value) 30 { 31 for (auto& opt : m_optlist) 32 if (!opt.flag.compare(flag)) { 33 opt.set = true; 34 opt.value = value; 35 } 36 } 37 38 bool 39 parser_t::isset(::std::string flag) 40 { 41 for (auto& opt : m_optlist) 42 if (!opt.flag.compare(flag)) { 43 return opt.set; 44 } 45 46 return false; 47 } 48 49 ::std::pair<int, ::std::vector<char*>> 50 parser_t::getargs() 51 { 52 ::std::vector<char*> raw; 53 for (auto& s : m_args) 54 raw.push_back(convert_new(s)); 55 56 return {raw.size(), raw}; 57 } 58 59 ::std::vector<option_t> 60 parser_t::getopts() 61 { 62 return m_optlist; 63 }