Add support for int list options, specified using a comma seperated list of int

This commit is contained in:
Maxime Coste 2013-03-06 14:12:56 +01:00
parent 3e7344fb14
commit 0e2ba188dc
2 changed files with 32 additions and 0 deletions

View File

@ -479,6 +479,8 @@ void declare_option(const CommandParameters& params, Context& context)
opt = &GlobalOptions::instance().declare_option<int>(params[1], 0);
else if (params[0] == "str")
opt = &GlobalOptions::instance().declare_option<String>(params[1], "");
else if (params[0] == "int-list")
opt = &GlobalOptions::instance().declare_option<std::vector<int>>(params[1], {});
else
throw runtime_error("unknown type " + params[0]);

View File

@ -26,6 +26,32 @@ void option_from_string(const String& str, bool& opt)
throw runtime_error("boolean values are either true, yes, false or no");
}
template<typename T>
String option_to_string(const std::vector<T>& opt)
{
String res;
for (size_t i = 0; i < opt.size(); ++i)
{
res += option_to_string(opt[i]);
if (i != opt.size() - 1)
res += ",";
}
return res;
}
template<typename T>
void option_from_string(const String& str, std::vector<T>& opt)
{
opt.clear();
std::vector<String> elems = split(str, ',');
for (auto& elem: elems)
{
T opt_elem;
option_from_string(elem, opt_elem);
opt.push_back(opt_elem);
}
}
}
template<typename T>
@ -79,6 +105,8 @@ template void Option::set<int>(const int&);
template const bool& Option::get<bool>() const;
template void Option::set<bool>(const bool&);
template const std::vector<int>& Option::get<std::vector<int>>() const;
template void Option::set<std::vector<int>>(const std::vector<int>&);
OptionManager::OptionManager(OptionManager& parent)
: m_parent(&parent)
@ -205,4 +233,6 @@ Option& GlobalOptions::declare_option(const String& name, const T& value)
return *m_options.back();
}
template Option& GlobalOptions::declare_option<>(const String&, const std::vector<int>&);
}