All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CLTool.h
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #ifndef __PLUMED_core_CLTool_h
23 #define __PLUMED_core_CLTool_h
24 #include <cstdio>
25 #include <vector>
26 #include <string>
27 #include <cstdio>
28 #include "tools/Tools.h"
29 #include "tools/Keywords.h"
30 #include <iostream>
31 
32 namespace PLMD{
33 
34 class Communicator;
35 
37  friend class CLTool;
38  friend class CLToolRegister;
39 private:
40  std::vector<std::string> line;
41 /// The documentation for this command line tool
42  const Keywords& keys;
44 public:
45  CLToolOptions(const std::string &name);
46  CLToolOptions(const CLToolOptions& co, const Keywords& k);
47 };
48 
49 /**
50 \ingroup INHERIT
51 This is the abstract base class to use for implementing new command line tool, within it there
52 is \ref AddingACLTool "information" as to how to go about implemneting a new tool.
53 */
54 
55 class CLTool {
56 private:
57 /// The name of this command line tool
58  const std::string name;
59 /// The list of keywords for this CLTool
61 /// The data read in from the command line stored in a map with the keywords
62  std::map<std::string,std::string> inputData;
63 /// Read the arguments from the command line
64  bool readCommandLineArgs( int argc, char**argv, FILE*out );
65 /// Read the arguments from an input file specified on the command line
66  bool readInputFile( int argc, char**argv, FILE* in, FILE*out );
67 /// Set arguments from the default options provided to Keywords
68  void setRemainingToDefault(FILE* out);
69 public:
70 /// Set the input data:
71  void setInputData(const std::map<std::string,std::string>&inputData){
72  this->inputData=inputData;
73  }
74  const std::map<std::string,std::string>&getInputData(){
75  return this->inputData;
76  }
77 protected:
78 /// Get the value of one of the command line arguments
79  template<class T>
80  bool parse(const std::string&key,T&t);
81 /// Find out whether one of the command line flags is present or not
82  void parseFlag(const std::string&key,bool&t);
83 /// Crash the command line tool with an error
84  void error(const std::string& msg);
85  template<class T>
86  bool parseVector(const std::string&key,std::vector<T>&t);
87 public:
88 /// How is the input specified on the command line or in an input file
90 /// Create the help keywords
91  static void registerKeywords( Keywords& keys );
92  CLTool(const CLToolOptions& co );
93 /// Read the arguments from the command line
94  bool readInput( int argc, char**argv, FILE* in, FILE*out );
95 /// virtual function mapping to the specific main for each tool
96  virtual int main( FILE* in, FILE*out, Communicator&pc )=0;
97 /// virtual function returning a one-line descriptor for the tool
98  virtual std::string description()const{return "(no description available)";}
99 /// virtual destructor to allow inheritance
100  virtual ~CLTool(){}
101 };
102 
103 template<class T>
104 bool CLTool::parse(const std::string&key,T&t){
105  plumed_massert(keywords.exists(key),"keyword " + key + " has not been registered");
106  if(keywords.style(key,"compulsory") ){
107  if(inputData.count(key)==0) error("missing data for keyword " + key);
108  bool check=Tools::convert(inputData[key],t);
109  if(!check) error("data input for keyword " + key + " has wrong type");
110  return true;
111  }
112  if( inputData.count(key)==0 ) return false;
113  Tools::convert(inputData[key],t);
114  return true;
115 }
116 // very limited support and check: take more from core/Action.h parseVector
117 template<class T>
118 bool CLTool::parseVector(const std::string&key,std::vector<T>&t){
119 
120  // Check keyword has been registered
121  plumed_massert(keywords.exists(key), "keyword " + key + " has not been registered");
122  // initial size
123  unsigned size=t.size();
124  bool skipcheck=false;
125  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
126 
127  // check if there is some value
128 
129  plumed_massert(inputData[key]!="false","compulsory keyword "+std::string(key)+"has no data");
130  std::vector<std::string> words=Tools::getWords(inputData[key],"\t\n ,");
131  t.resize(0);
132  if(words.size()==0)return false;
133 
134  for(unsigned i=0;i<words.size();++i){
135  T v;
136  Tools::convert(words[i],v);
137  t.push_back(v);
138  }
139  // check the size
140  if( !skipcheck && t.size()!=size ) {
141  plumed_merror("vector read in for keyword "+key+" has wrong size" );
142  }
143  std::string def;
144  T val;
145  if ( keywords.style(key,"compulsory") && t.size()==0 ){
146  if( keywords.getDefaultValue(key,def) ){
147  if( def.length()==0 || !Tools::convert(def,val) ){
148  plumed_merror("ERROR in keyword "+key+ " has weird default value" );
149  } else {
150  for(unsigned i=0;i<t.size();++i) t[i]=val;
151  }
152  } else {
153  plumed_merror("keyword " + key + " is compulsory for this action");
154  }
155  }
156  return true;
157 }
158 
159 
160 
161 }
162 
163 #endif
static void registerKeywords(Keywords &keys)
Create the help keywords.
Definition: CLTool.cpp:40
void setRemainingToDefault(FILE *out)
Set arguments from the default options provided to Keywords.
Definition: CLTool.cpp:128
bool readInput(int argc, char **argv, FILE *in, FILE *out)
Read the arguments from the command line.
Definition: CLTool.cpp:60
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
virtual int main(FILE *in, FILE *out, Communicator &pc)=0
virtual function mapping to the specific main for each tool
Class containing wrappers to MPI.
Definition: Communicator.h:44
std::vector< std::string > line
Definition: CLTool.h:40
bool parseVector(const std::string &key, std::vector< T > &t)
Definition: CLTool.h:118
bool getDefaultValue(std::string key, std::string &def) const
Get the value of the default for the keyword named key.
Definition: Keywords.cpp:543
CLToolOptions(const std::string &name)
Definition: CLTool.cpp:28
This is the abstract base class to use for implementing new command line tool, within it there is inf...
Definition: CLTool.h:55
This class holds the keywords and their documentation.
Definition: Keywords.h:36
bool readInputFile(int argc, char **argv, FILE *in, FILE *out)
Read the arguments from an input file specified on the command line.
Definition: CLTool.cpp:146
const std::string name
The name of this command line tool.
Definition: CLTool.h:58
const Keywords & keys
The documentation for this command line tool.
Definition: CLTool.h:42
static std::vector< std::string > getWords(const std::string &line, const char *sep=NULL, int *parlevel=NULL, const char *parenthesis="{")
Split the line in words using separators.
Definition: Tools.cpp:112
CLTool(const CLToolOptions &co)
Definition: CLTool.cpp:44
const Keywords & keywords
The list of keywords for this CLTool.
Definition: CLTool.h:60
void setInputData(const std::map< std::string, std::string > &inputData)
Set the input data:
Definition: CLTool.h:71
bool style(const std::string &k, const std::string &t) const
Check if the keyword with name k has style t.
Definition: Keywords.cpp:223
bool exists(const std::string &k) const
Check if there is a keyword with name k.
Definition: Keywords.cpp:239
bool parse(const std::string &key, T &t)
Get the value of one of the command line arguments.
Definition: CLTool.h:104
void parseFlag(const std::string &key, bool &t)
Find out whether one of the command line flags is present or not.
Definition: CLTool.cpp:51
virtual std::string description() const
virtual function returning a one-line descriptor for the tool
Definition: CLTool.h:98
virtual ~CLTool()
virtual destructor to allow inheritance
Definition: CLTool.h:100
Same as ActionRegister, but for CLTools.
std::map< std::string, std::string > inputData
The data read in from the command line stored in a map with the keywords.
Definition: CLTool.h:62
enum PLMD::CLTool::@0 inputdata
How is the input specified on the command line or in an input file.
void error(const std::string &msg)
Crash the command line tool with an error.
Definition: CLTool.cpp:206
bool readCommandLineArgs(int argc, char **argv, FILE *out)
Read the arguments from the command line.
Definition: CLTool.cpp:70
const std::map< std::string, std::string > & getInputData()
Definition: CLTool.h:74
static Keywords emptyKeys
Definition: CLTool.h:43