option_map: Diagnose unused option on request
* spot/misc/optionmap.hh, spot/misc/optionmap.cc (report_unused_options, set_, set_set_): New methods. * bin/autfilt.cc, bin/dstar2tgba.cc, bin/ltl2tgba.cc, bin/ltl2tgta.cc: Call report_unused_options(). * tests/core/ltlcross2.test, tests/core/readsave.test: Fix typos in options. * tests/core/minusx.test: New file. * tests/Makefile.am: Add it. * NEWS: Mention this.
This commit is contained in:
parent
f7f353db68
commit
e419150c30
11 changed files with 133 additions and 30 deletions
|
|
@ -1,6 +1,6 @@
|
|||
// -*- coding: utf-8 -*-
|
||||
// Copyright (C) 2008, 2013, 2014, 2015 Laboratoire de Recherche et
|
||||
// Développement de l'Epita (LRDE).
|
||||
// Copyright (C) 2008, 2013, 2014, 2015, 2016 Laboratoire de Recherche
|
||||
// et Développement de l'Epita (LRDE).
|
||||
// Copyright (C) 2005 Laboratoire d'Informatique de Paris 6 (LIP6),
|
||||
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
|
||||
// et Marie Curie.
|
||||
|
|
@ -23,6 +23,7 @@
|
|||
#include "config.h"
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
#include <spot/misc/optionmap.hh>
|
||||
|
||||
|
|
@ -69,7 +70,7 @@ namespace spot
|
|||
|
||||
if (*options != '=')
|
||||
{
|
||||
options_[name] = (negated ? 0 : 1);
|
||||
set_(name, !negated);
|
||||
}
|
||||
else if (negated)
|
||||
{
|
||||
|
|
@ -93,8 +94,7 @@ namespace spot
|
|||
while (*options && *options != sep);
|
||||
if (*options != sep)
|
||||
return start - 1;
|
||||
std::string val(start, options);
|
||||
options_str_[name] = val;
|
||||
set_str_(name, std::string(start, options));
|
||||
if (*options)
|
||||
++options;
|
||||
}
|
||||
|
|
@ -120,7 +120,7 @@ namespace spot
|
|||
return options;
|
||||
}
|
||||
options = val_end;
|
||||
options_[name] = val;
|
||||
set_(name, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -130,6 +130,7 @@ namespace spot
|
|||
int
|
||||
option_map::get(const char* option, int def) const
|
||||
{
|
||||
unused_.erase(option);
|
||||
auto it = options_.find(option);
|
||||
return (it == options_.end()) ? def : it->second;
|
||||
}
|
||||
|
|
@ -137,21 +138,28 @@ namespace spot
|
|||
std::string
|
||||
option_map::get_str(const char* option, std::string def) const
|
||||
{
|
||||
unused_.erase(option);
|
||||
auto it = options_str_.find(option);
|
||||
return (it == options_str_.end()) ? def : it->second;
|
||||
}
|
||||
|
||||
int
|
||||
option_map::operator[](const char* option) const
|
||||
void option_map::set_(const std::string& option, int val)
|
||||
{
|
||||
return get(option);
|
||||
options_[option] = val;
|
||||
unused_.insert(option);
|
||||
}
|
||||
|
||||
void option_map::set_str_(const std::string& option, const std::string& val)
|
||||
{
|
||||
options_str_[option] = val;
|
||||
unused_.insert(option);
|
||||
}
|
||||
|
||||
int
|
||||
option_map::set(const char* option, int val, int def)
|
||||
{
|
||||
int old = get(option, def);
|
||||
options_[option] = val;
|
||||
set_(option, val);
|
||||
return old;
|
||||
}
|
||||
|
||||
|
|
@ -159,7 +167,7 @@ namespace spot
|
|||
option_map::set_str(const char* option, std::string val, std::string def)
|
||||
{
|
||||
std::string old = get_str(option, def);
|
||||
options_str_[option] = val;
|
||||
set_str_(option, val);
|
||||
return old;
|
||||
}
|
||||
|
||||
|
|
@ -168,11 +176,20 @@ namespace spot
|
|||
{
|
||||
options_ = o.options_;
|
||||
options_str_ = o.options_str_;
|
||||
unused_ = o.unused_;
|
||||
}
|
||||
|
||||
int
|
||||
option_map::operator[](const char* option) const
|
||||
{
|
||||
unused_.erase(option);
|
||||
return get(option);
|
||||
}
|
||||
|
||||
int&
|
||||
option_map::operator[](const char* option)
|
||||
{
|
||||
unused_.erase(option);
|
||||
return options_[option];
|
||||
}
|
||||
|
||||
|
|
@ -185,4 +202,24 @@ namespace spot
|
|||
os << '"' << p.first << "\" = \"" << p.second << "\"\n";
|
||||
return os;
|
||||
}
|
||||
|
||||
void option_map::report_unused_options() const
|
||||
{
|
||||
auto s = unused_.size();
|
||||
if (s == 0U)
|
||||
return;
|
||||
std::ostringstream os;
|
||||
if (s == 1U)
|
||||
{
|
||||
os << "option '" << *unused_.begin()
|
||||
<< "' was not used (possible typo?)";
|
||||
}
|
||||
else
|
||||
{
|
||||
os << "the following options where not used (possible typos?):";
|
||||
for (auto opt: unused_)
|
||||
os << "\n\t- '" << opt << '\'';
|
||||
}
|
||||
throw std::runtime_error(os.str());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
// -*- coding: utf-8 -*-
|
||||
// Copyright (C) 2013, 2015 Laboratoire de Recherche et Developpement de
|
||||
// l'Epita (LRDE)
|
||||
// Copyright (C) 2013, 2015, 2016 Laboratoire de Recherche et
|
||||
// Developpement de l'Epita (LRDE)
|
||||
// Copyright (C) 2005 Laboratoire d'Informatique de Paris 6 (LIP6),
|
||||
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
|
||||
// et Marie Curie.
|
||||
|
|
@ -26,6 +26,7 @@
|
|||
#include <string>
|
||||
#include <map>
|
||||
#include <iosfwd>
|
||||
#include <set>
|
||||
|
||||
namespace spot
|
||||
{
|
||||
|
|
@ -86,6 +87,9 @@ namespace spot
|
|||
std::string set_str(const char* option,
|
||||
std::string val, std::string def = {});
|
||||
|
||||
/// \brief Raise a runtime_error if some option haven't been used.
|
||||
void report_unused_options() const;
|
||||
|
||||
/// Acquire all the settings of \a o.
|
||||
void set(const option_map& o);
|
||||
|
||||
|
|
@ -99,5 +103,12 @@ namespace spot
|
|||
private:
|
||||
std::map<std::string, int> options_;
|
||||
std::map<std::string, std::string> options_str_;
|
||||
// Unused values. Initially they will store all options, and they
|
||||
// will be erased as they are used. The resulting set can be used
|
||||
// for diagnosing errors.
|
||||
mutable std::set<std::string> unused_;
|
||||
|
||||
void set_(const std::string&, int val);
|
||||
void set_str_(const std::string&, const std::string& val);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue