Skip to content

Instantly share code, notes, and snippets.

@bananu7
Created November 12, 2014 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bananu7/f7f28b8f0d231078f21d to your computer and use it in GitHub Desktop.
Save bananu7/f7f28b8f0d231078f21d to your computer and use it in GitHub Desktop.
std::map<std::string, std::string> parseOptions(std::vector<std::string> const& args) {
std::map<std::string, std::string> options;
auto it = args.begin();
it++; // skip the file name
while(it != args.end()) {
if (it->at(0) == '-') {
std::string key = *it;
it++;
if (it == args.end()) {
options[key] = "";
break;
}
std::string val = "";
if (it->at(0) != '-') {
val = *it;
it++;
}
options[key] = val;
}
else {
throw std::runtime_error("Stray string in commandline");
}
}
return options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment