diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2013-03-06 14:12:56 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2013-03-06 14:27:00 +0100 |
| commit | 0e2ba188dc1bfefa0bd7e064aa7d67d7ea0784a7 (patch) | |
| tree | bda406dc82ebf78777cef65ad64276c281782bc2 /src | |
| parent | 3e7344fb14060931a6a6cd13b367ce13adbe6b37 (diff) | |
Add support for int list options, specified using a comma seperated list of int
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands.cc | 2 | ||||
| -rw-r--r-- | src/option_manager.cc | 30 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/commands.cc b/src/commands.cc index 6dce3986..fc315fd3 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -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]); diff --git a/src/option_manager.cc b/src/option_manager.cc index 5f070ad8..817d5409 100644 --- a/src/option_manager.cc +++ b/src/option_manager.cc @@ -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>&); + } |
