How to use unknown keys using YAML-cpp?

A use case is to go through a configuration file written in YAML. I need to check each key and analyze its value accordingly. I like the idea of ​​using random access methods, for example doc["key"] >> value, but I really need to do this to warn the user about unrecognized keys in the configuration file if, for example, they mistakenly wrote the key. I do not know how to do this without repeating the file.

I know I can do this with YAML::Iterator, so

for (YAML::Iterator it=doc.begin(); it<doc.end(); ++it) 
{ 
   std::string key;
   it.first() >> key;
   if (key=="parameter") { /* do stuff, possibly iterating over nested keys */ }
   } else if (/* */) {
   } else {
       std::cerr << "Warning: bad parameter" << std::endl;
   }
}

but is there an easier way to do this? My path seems to completely bypass any error checking built into YAML-cpp, and seems to undo all the simplicity of random key access.

+1
1

, , , FindValue:

if(const YAML::Node *pNode = doc.FindValue("parameter")) {
   // do something
} else {
   std::cerr << "Parameter missing\n";
}

, , .

+2

All Articles