option_map: Don't report unused options if option_map is not used

* spot/misc/optionmap.cc, spot/misc/optionmap.hh: here.
This commit is contained in:
Florian Renkin 2022-03-22 14:46:31 +01:00
parent 8d9597d80d
commit 0a6b627914
2 changed files with 23 additions and 16 deletions

View file

@ -130,6 +130,7 @@ namespace spot
int int
option_map::get(const char* option, int def) const option_map::get(const char* option, int def) const
{ {
is_used_ = true;
unused_.erase(option); unused_.erase(option);
auto it = options_.find(option); auto it = options_.find(option);
return (it == options_.end()) ? def : it->second; return (it == options_.end()) ? def : it->second;
@ -138,6 +139,7 @@ namespace spot
std::string std::string
option_map::get_str(const char* option, std::string def) const option_map::get_str(const char* option, std::string def) const
{ {
is_used_ = true;
unused_.erase(option); unused_.erase(option);
auto it = options_str_.find(option); auto it = options_str_.find(option);
return (it == options_str_.end()) ? def : it->second; return (it == options_str_.end()) ? def : it->second;
@ -226,21 +228,25 @@ namespace spot
void option_map::report_unused_options() const void option_map::report_unused_options() const
{ {
auto s = unused_.size(); // We don't consider that an unused map has unused options.
if (s == 0U) if (is_used_)
return; {
std::ostringstream os; auto s = unused_.size();
if (s == 1U) if (s == 0U)
{ return;
os << "option '" << *unused_.begin() std::ostringstream os;
<< "' was not used (possible typo?)"; if (s == 1U)
} {
else os << "option '" << *unused_.begin()
{ << "' was not used (possible typo?)";
os << "the following options where not used (possible typos?):"; }
for (auto opt: unused_) else
os << "\n\t- '" << opt << '\''; {
} os << "the following options where not used (possible typos?):";
throw std::runtime_error(os.str()); for (auto opt: unused_)
os << "\n\t- '" << opt << '\'';
}
throw std::runtime_error(os.str());
}
} }
} }

View file

@ -110,6 +110,7 @@ namespace spot
// will be erased as they are used. The resulting set can be used // will be erased as they are used. The resulting set can be used
// for diagnosing errors. // for diagnosing errors.
mutable std::set<std::string> unused_; mutable std::set<std::string> unused_;
mutable bool is_used_ = false;
void set_(const std::string&, int val); void set_(const std::string&, int val);
void set_str_(const std::string&, const std::string& val); void set_str_(const std::string&, const std::string& val);