Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-2023 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.org for more information.
6 :
7 : This file is part of plumed, version 2.
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 : #include "CLTool.h"
23 :
24 : namespace PLMD {
25 :
26 : Keywords CLToolOptions::emptyKeys;
27 :
28 4127 : CLToolOptions::CLToolOptions(const std::string &name):
29 4127 : line(1,name),
30 4127 : keys(emptyKeys) {
31 4127 : }
32 :
33 4127 : CLToolOptions::CLToolOptions(const CLToolOptions& co, const Keywords& k):
34 4127 : line(co.line),
35 4127 : keys(k) {
36 4127 : }
37 :
38 107987 : void CLTool::registerKeywords( Keywords& keys ) {
39 107987 : keys.addFlag("--help/-h",false,"print this help");
40 107987 : }
41 :
42 4127 : CLTool::CLTool(const CLToolOptions& co ):
43 4127 : name(co.line[0]),
44 4127 : keywords(co.keys),
45 4127 : inputdata(inputType::unset) {
46 4127 : }
47 :
48 27887 : void CLTool::parseFlag( const std::string&key, bool&t ) {
49 27887 : plumed_massert(keywords.exists(key),"keyword " + key + " has not been registered");
50 55774 : plumed_massert(keywords.style(key,"flag"),"keyword " + key + " has not been registered as a flag");
51 0 : plumed_assert(inputData.count(key)>0);
52 55774 : if( inputData[key]=="true") {
53 3037 : t=true;
54 49700 : } else if( inputData[key]=="false") {
55 24850 : t=false;
56 : } else {
57 0 : plumed_error();
58 : }
59 27887 : }
60 :
61 4032 : bool CLTool::readInput( int argc, char**argv, FILE* in, FILE*out ) {
62 4032 : plumed_massert( inputdata!=inputType::unset,"You have not specified where your tool reads its input. "
63 : "If it is from the command line (like driver) add inputdata=inputType::commandline to the "
64 : "tools constructor. If it reads everything from an input file (like simplemd) "
65 : "add inputdata=inputType::ifile to the tools constructor");
66 4032 : if(inputdata==inputType::commandline) {
67 3980 : return readCommandLineArgs( argc, argv, out );
68 : }
69 52 : if(inputdata==inputType::ifile) {
70 52 : return readInputFile( argc, argv, in, out );
71 : }
72 : return true;
73 : }
74 :
75 3980 : bool CLTool::readCommandLineArgs( int argc, char**argv, FILE*out ) {
76 3980 : plumed_assert(inputdata==inputType::commandline);
77 3980 : std::string prefix(""), a(""), thiskey;
78 :
79 : // Set all flags to default false
80 73543 : for(unsigned k=0; k<keywords.size(); ++k) {
81 69563 : thiskey=keywords.getKeyword(k);
82 139126 : if( keywords.style(thiskey,"flag") ) {
83 64564 : inputData.insert(std::pair<std::string,std::string>(thiskey,"false"));
84 : }
85 : }
86 :
87 : // Read command line arguments
88 : bool printhelp=false;
89 16066 : for(int i=1; i<argc; i++) {
90 24172 : a=prefix+argv[i];
91 12086 : if(a.length()==0) {
92 0 : continue;
93 : }
94 24172 : if(a=="-h" || a=="--help") {
95 : printhelp=true;
96 : } else {
97 : bool found=false;
98 389000 : for(unsigned k=0; k<keywords.size(); ++k) {
99 376914 : thiskey=keywords.getKeyword(k);
100 753828 : if( keywords.style(thiskey,"flag") ) {
101 77602 : if( a==thiskey ) {
102 : found=true;
103 3035 : inputData[thiskey]="true";
104 : }
105 : } else {
106 299312 : if( a==thiskey ) {
107 4186 : prefix=thiskey+"=";
108 : found=true;
109 8372 : inputData.insert(std::pair<std::string,std::string>(thiskey,""));
110 590252 : } else if(Tools::startWith(a,thiskey+"=")) {
111 4865 : a.erase(0,a.find("=")+1);
112 : prefix="";
113 : found=true;
114 : if(inputData.count(thiskey)==0) {
115 1360 : inputData.insert(std::pair<std::string,std::string>(thiskey,a));
116 : } else {
117 4185 : inputData[thiskey]=a;
118 : }
119 : }
120 : }
121 : }
122 12086 : if(!found) {
123 0 : std::fprintf(stderr,"ERROR in input for command line tool %s : %s option is unknown \n\n", name.c_str(), a.c_str() );
124 : std::fprintf(out,"Usage: %s < inputFile \n", name.c_str() );
125 : std::fprintf(out,"inputFile should contain one directive per line. The directives should come from amongst the following\n\n");
126 0 : keywords.print( out );
127 : printhelp=true;
128 : }
129 : }
130 12086 : if(printhelp) {
131 : break;
132 : }
133 : }
134 :
135 3980 : if(!printhelp) {
136 3980 : setRemainingToDefault(out);
137 : }
138 :
139 3980 : if(printhelp) {
140 : std::fprintf(out,"Usage: %s [options] \n\n", name.c_str() );
141 0 : keywords.print( out );
142 : }
143 :
144 3980 : return !printhelp;
145 : }
146 :
147 4032 : void CLTool::setRemainingToDefault(FILE* out) {
148 : std::string def, thiskey;
149 74615 : for(unsigned k=0; k<keywords.size(); ++k) {
150 70583 : thiskey=keywords.getKeyword(k);
151 141166 : if( keywords.style(thiskey,"compulsory") ) {
152 : if( inputData.count(thiskey)==0 ) {
153 2089 : if( keywords.getDefaultValue(thiskey,def) ) {
154 2089 : plumed_assert( def.length()>0 );
155 4178 : inputData.insert(std::pair<std::string,std::string>(thiskey,def));
156 : } else {
157 : std::fprintf(out,"ERROR : argument %s is compulsory. Use --help option for help\n",thiskey.c_str() );
158 0 : plumed_error();
159 : }
160 : }
161 : }
162 : }
163 4032 : }
164 :
165 52 : bool CLTool::readInputFile( int argc, char**argv, FILE* in, FILE*out ) {
166 52 : plumed_assert(inputdata==inputType::ifile);
167 :
168 : // Check if use is just asking for help
169 : std::string a;
170 95 : for(int i=1; i<argc; i++) {
171 43 : a=argv[i];
172 43 : if(a.length()==0) {
173 0 : continue;
174 : }
175 86 : if(a=="-h" || a=="--help") {
176 : std::fprintf(out,"Usage: %s < inputFile \n", name.c_str() );
177 : std::fprintf(out,"inputFile should contain one directive per line. The directives should come from amongst the following\n\n");
178 0 : keywords.print( out );
179 : return false;
180 : }
181 : }
182 :
183 : FILE* mystdin=in;
184 : // call fclose when fp_deleter goes out of scope
185 : auto deleter=[](auto f) {
186 43 : std::fclose(f);
187 43 : };
188 : std::unique_ptr<FILE,decltype(deleter)> fp_deleter(nullptr,deleter);
189 :
190 52 : if(argc==2) {
191 43 : mystdin=std::fopen(argv[1],"r");
192 43 : if(!mystdin) {
193 0 : std::fprintf(stderr,"ERROR: cannot open file %s\n\n",argv[1]);
194 : std::fprintf(out,"Usage: %s < inputFile \n", name.c_str() );
195 : std::fprintf(out,"inputFile should contain one directive per line. The directives should come from amongst the following\n\n");
196 0 : keywords.print( out );
197 : return false;
198 : }
199 : fp_deleter.reset(mystdin);
200 : }
201 :
202 52 : plumed_assert(mystdin);
203 :
204 : char buffer[256];
205 : std::string line;
206 : line.resize(256);
207 863 : while(fgets(buffer,256,mystdin)) {
208 : line=buffer;
209 24504 : for(unsigned i=0; i<line.length(); ++i)
210 23693 : if(line[i]=='#' || line[i]=='\n') {
211 811 : line.erase(i);
212 : }
213 811 : Tools::stripLeadingAndTrailingBlanks( line );
214 811 : if(line.length()==0) {
215 37 : continue;
216 : }
217 774 : std::sscanf(line.c_str(),"%255s",buffer);
218 774 : std::string keyword=buffer;
219 : bool found=false;
220 16338 : for(unsigned i=0; i<keywords.size(); ++i) {
221 15564 : std::string thiskey=keywords.getKeyword(i);
222 15564 : if(thiskey==keyword) {
223 : found=true;
224 774 : std::size_t keypos=line.find_first_of(keyword)+keyword.length();
225 1548 : inputData.insert(std::pair<std::string,std::string>(thiskey,line.substr(keypos)));
226 774 : Tools::stripLeadingAndTrailingBlanks( inputData[thiskey] );
227 : }
228 : }
229 774 : if(!found) {
230 0 : std::fprintf(stderr,"ERROR in input for command line tool %s : unknown keyword %s found in input file\n\n",name.c_str(),keyword.c_str());
231 : std::fprintf(out,"Usage: %s < inputFile \n", name.c_str() );
232 : std::fprintf(out,"inputFile should contain one directive per line. The directives should come from amongst the following\n\n");
233 0 : keywords.print( out );
234 : return false;
235 : }
236 : }
237 :
238 52 : setRemainingToDefault(out);
239 : return true;
240 : }
241 :
242 0 : [[noreturn]] void CLTool::error( const std::string& msg ) {
243 0 : std::fprintf(stderr,"ERROR : in input for command line tool %s : %s\n",name.c_str(),msg.c_str());
244 0 : plumed_error();
245 : }
246 :
247 : }
|