24 #ifndef CHOICES_145313413_H 25 #define CHOICES_145313413_H 29 typedef std::map<std::string, std::string> options;
31 inline std::ostream& operator<<(std::ostream& stm,
const options& opt)
33 options::const_iterator it;
35 for(it = opt.begin(); it != opt.end(); it++)
37 stm << (*it).first <<
": " << (*it).second <<
" ";
40 stm <<
"}" << std::endl;
47 inline void as_vect(
int argc,
const char *argv[], std::vector<std::string>& res)
49 res.assign(argv, argv + argc);
52 inline std::string remove_prefix(
const std::string s)
54 size_t pos = s.find(
"--");
55 return pos == std::string::npos ? s : s.substr(pos + 2);
58 inline bool is_assignment(
const std::string& s)
60 return s.find(
"=") != std::string::npos;
63 inline bool is_flag(
const std::string& s)
65 return s.find(
"--") == 0 && !is_assignment(s);
68 inline std::string flag(
const std::string& assignment)
70 std::string s = remove_prefix(assignment);
71 return s.substr(0, s.find(
"="));
74 inline std::string value(
const std::string& assignment)
76 return assignment.substr(assignment.find(
"=")+1);
79 inline void get_options(
const std::vector<std::string> v, options& opt)
81 std::vector<std::string>::const_iterator it;
82 for(it = v.begin(); it != v.end(); it++)
86 opt[remove_prefix(*it)] =
"";
88 else if(is_assignment(*it))
90 opt[flag(*it)] = value(*it);
96 namespace d = details;
98 inline options parse_cmd(
int argc,
const char *argv[])
101 if(argc == 0 || argv == 0)
106 std::vector<std::string> v;
107 d::as_vect(argc, argv, v);
111 d::get_options(v, o);
116 inline bool has_option(
const std::string& option,
const options& opt)
118 return opt.find(option) != opt.end();
121 inline const std::string& option_value(
const std::string& option,
const options& opt)
123 options::const_iterator it = opt.find(option);