Skip to content

Commit 021367d

Browse files
authored
Merge pull request #161 from zhaozhiwen/fix/125-yaml-detection
Detect YAML inputs by extension and report a missing file cleanly
2 parents 410a6be + 139e022 commit 021367d

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

gemc/goptions/goptions.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,15 @@ void GOptions::printOptionOrSwitchHelp(const std::string& tag) const {
284284
// Private method: see header. Kept undocumented here to avoid duplicate param docs.
285285
vector<string> GOptions::findYamls(int argc, char* argv[]) {
286286
vector<string> yaml_files;
287+
auto ends_with = [](const string& s, const string& suffix) {
288+
return s.size() >= suffix.size() &&
289+
s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
290+
};
287291
for (int i = 1; i < argc; i++) {
288292
string arg = argv[i];
289-
size_t pos = arg.find(".yaml");
290-
if (pos != string::npos) yaml_files.push_back(arg);
291-
pos = arg.find(".yml");
292-
if (pos != string::npos) yaml_files.push_back(arg);
293+
// Match a trailing .yaml/.yml extension, not any substring, so option values such as
294+
// -prefix=run.yaml.bak are not mistaken for input files (and silently dropped).
295+
if (ends_with(arg, ".yaml") || ends_with(arg, ".yml")) yaml_files.push_back(arg);
293296
}
294297
return yaml_files;
295298
}
@@ -310,6 +313,11 @@ void GOptions::setOptionsValuesFromYamlFile(const std::string& yaml) {
310313
try {
311314
config = YAML::LoadFile(yaml);
312315
}
316+
catch (YAML::BadFile& e) {
317+
cerr << FATALERRORL << "Cannot open yaml file " << YELLOWHHL << yaml << RSTHHR
318+
<< ". Check the path and spelling." << endl;
319+
exit(EC__YAML_PARSING_ERROR);
320+
}
313321
catch (YAML::ParserException& e) {
314322
cerr << FATALERRORL << "Error parsing " << YELLOWHHL << yaml << RSTHHR
315323
<< " yaml file." << endl;

0 commit comments

Comments
 (0)