From a0ad97d8b50b10190ade1b98f3510a2289d77838 Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Wed, 23 Jan 2013 13:42:33 +0100
Subject: [PATCH] Added tool to parse a vector from cltools

---
 src/core/CLTool.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/src/core/CLTool.h b/src/core/CLTool.h
index 4362829e3..a414623ae 100644
--- a/src/core/CLTool.h
+++ b/src/core/CLTool.h
@@ -27,6 +27,7 @@
 #include <cstdio>
 #include "tools/Tools.h"
 #include "tools/Keywords.h"
+#include <iostream>
 
 namespace PLMD{
 
@@ -81,6 +82,8 @@ protected:
   void parseFlag(const std::string&key,bool&t);  
 /// Crash the command line tool with an error
   void error(const std::string& msg);
+  template<class T>
+  bool parseVector(const std::string&key,std::vector<T>&t);  
 public:
 /// How is the input specified on the command line or in an input file
   enum {unset,commandline,ifile} inputdata;
@@ -110,6 +113,50 @@ bool CLTool::parse(const std::string&key,T&t){
   Tools::convert(inputData[key],t);
   return true;
 }
+// very limited support and check: take more from core/Action.h parseVector  
+template<class T>
+bool CLTool::parseVector(const std::string&key,std::vector<T>&t){
+
+  // Check keyword has been registered
+  plumed_massert(keywords.exists(key), "keyword " + key + " has not been registered");
+  // initial size
+  unsigned size=t.size(); 
+  bool skipcheck=false;
+  if(size==0) skipcheck=true; // if the vector in input has size zero, skip the check if size of input vector is the same of argument read 
+
+  // check if there is some value
+
+  plumed_massert(inputData[key]!="false","compulsory keyword "+std::string(key)+"has no data");
+  std::vector<std::string> words=Tools::getWords(inputData[key],"\t\n ,");
+  t.resize(0); 
+  if(words.size()==0)return false;
+
+  for(unsigned i=0;i<words.size();++i){
+    T v;
+    Tools::convert(words[i],v);
+    t.push_back(v);
+  }
+  // check the size
+  if( !skipcheck && t.size()!=size ) {
+     plumed_merror("vector read in for keyword  "+key+" has wrong size" );
+  }
+  std::string def;
+  T val;
+  if ( keywords.style(key,"compulsory") && t.size()==0 ){
+       if( keywords.getDefaultValue(key,def) ){
+          if( def.length()==0 || !Tools::convert(def,val) ){
+             plumed_merror("ERROR in keyword "+key+ " has weird default value" );
+          } else {
+             for(unsigned i=0;i<t.size();++i) t[i]=val;
+          }          
+       } else {
+          plumed_merror("keyword " + key + " is compulsory for this action");
+       }
+  } 
+  return true; 
+}
+
+
 
 }
 
-- 
GitLab