Skip to content

Instantly share code, notes, and snippets.

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 eyalroz/011060ea931e1704d0dbc60cedc6d5b0 to your computer and use it in GitHub Desktop.
Save eyalroz/011060ea931e1704d0dbc60cedc6d5b0 to your computer and use it in GitHub Desktop.
static void add_unrecognized_options(TestConfiguration& config, const std::vector<string>& unrecognized_options)
{
// TODO: make sure all unrecognized options are of the form --option=value
// I'm sure the following can be made more elegant...
enum { OptionName, OptionValue } now_expecting = OptionName;
std::string option_name;
for(auto it = unrecognized_options.begin(); it != unrecognized_options.end(); it++) {
std::string option_value;
if (now_expecting == OptionValue) {
option_value = *it;
// Note! Not checking for '=' or ' ' characters in option values, if we know it's a value
if (option_value.find_first_not_of('-') == 2) {
// Got another option instead of a value, must be a flag option
// std::cout << "Inserting " << option_name << " as empty" << std::endl;
config.extra_options.insert(make_pair(option_name, std::string()));
option_name = option_value;
now_expecting = OptionValue;
continue;
}
}
else {
std::string s(*it);
auto key_starts = s.find_first_not_of('-');
util::enforce(key_starts != string::npos, "Couldn't determine option name for " + s);
auto option_name_ends = s.find_first_of(" =\n", key_starts);
if (option_name_ends == string::npos) {
now_expecting = OptionValue;
option_name = s.substr(key_starts);
continue;
}
option_name = s.substr(key_starts, option_name_ends - key_starts);
auto value_starts = s.find_first_not_of(" =\n", option_name_ends + 1);
if (value_starts == string::npos) {
now_expecting = OptionValue;
continue;
}
// string value = s.substr(value_starts);
option_value = s.substr(value_starts);
}
config.extra_options.insert(make_pair(option_name, option_value));
now_expecting = OptionName;
}
if (now_expecting != OptionName) {
config.extra_options.insert(make_pair(option_name, std::string()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment