瀏覽代碼

feat: add handling for map<string, V>

Sam Jaffe 3 年之前
父節點
當前提交
1948e00ed1
共有 1 個文件被更改,包括 17 次插入1 次删除
  1. 17 1
      include/program_args/utilities.h

+ 17 - 1
include/program_args/utilities.h

@@ -83,7 +83,23 @@ struct conversion_helper<std::vector<T>> : conversion_helper<T> {
   std::vector<T> operator()(std::vector<std::string> const & data) const {
     std::vector<T> rval;
     for (auto & str : data) {
-      rval.emplace_back((*this)(str));
+      rval.push_back((*this)(str));
+    }
+    return rval;
+  }
+};
+
+template <typename T>
+struct conversion_helper<std::map<std::string, T>> : conversion_helper<T> {
+  using conversion_helper<T>::operator();
+  std::map<std::string, T> operator()(std::vector<std::string> const & data) const {
+    std::map<std::string, T> rval;
+    for (auto & str : data) {
+      size_t pos = str.find('=');
+      if (pos == std::string::npos) {
+        throw std::invalid_argument("expected argument of the form key=value, got " + str);
+      }
+      rval.emplace(str.substr(0, pos), (*this)(str.substr(pos + 1)));
     }
     return rval;
   }