LCOV - code coverage report
Current view: top level - core - CLTool.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 96 112 85.7 %
Date: 2025-03-25 09:33:27 Functions: 9 10 90.0 %

          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        3954 : CLToolOptions::CLToolOptions(const std::string &name):
      29        3954 :   line(1,name),
      30        3954 :   keys(emptyKeys) {
      31        3954 : }
      32             : 
      33        3954 : CLToolOptions::CLToolOptions(const CLToolOptions& co, const Keywords& k):
      34        3954 :   line(co.line),
      35        3954 :   keys(k) {
      36        3954 : }
      37             : 
      38       92116 : void CLTool::registerKeywords( Keywords& keys ) {
      39       92116 :   keys.addFlag("--help/-h",false,"print this help");
      40       92116 : }
      41             : 
      42        3954 : CLTool::CLTool(const CLToolOptions& co ):
      43        3954 :   name(co.line[0]),
      44        3954 :   keywords(co.keys),
      45        3954 :   inputdata(unset) {
      46        3954 : }
      47             : 
      48       26542 : void CLTool::parseFlag( const std::string&key, bool&t ) {
      49       26542 :   plumed_massert(keywords.exists(key),"keyword " + key + " has not been registered");
      50       53084 :   plumed_massert(keywords.style(key,"flag"),"keyword " + key + " has not been registered as a flag");
      51           0 :   plumed_assert(inputData.count(key)>0);
      52       53084 :   if( inputData[key]=="true") {
      53        2886 :     t=true;
      54       47312 :   } else if( inputData[key]=="false") {
      55       23656 :     t=false;
      56             :   } else {
      57           0 :     plumed_error();
      58             :   }
      59       26542 : }
      60             : 
      61        3859 : bool CLTool::readInput( int argc, char**argv, FILE* in, FILE*out ) {
      62        3859 :   plumed_massert( inputdata!=unset,"You have not specified where your tool reads its input. "
      63             :                   "If it is from the command line (like driver) add inputdata=commandline to the "
      64             :                   "tools constructor. If it reads everything from an input file (like simplemd) "
      65             :                   "add inputdata=ifile to the tools constructor");
      66        3859 :   if(inputdata==commandline) {
      67        3807 :     return readCommandLineArgs( argc, argv, out );
      68             :   }
      69          52 :   if(inputdata==ifile) {
      70          52 :     return readInputFile( argc, argv, in, out );
      71             :   }
      72             :   return true;
      73             : }
      74             : 
      75        3807 : bool CLTool::readCommandLineArgs( int argc, char**argv, FILE*out ) {
      76        3807 :   plumed_assert(inputdata==commandline);
      77        3807 :   std::string prefix(""), a(""), thiskey;
      78             : 
      79             :   // Set all flags to default false
      80       70895 :   for(unsigned k=0; k<keywords.size(); ++k) {
      81       67088 :     thiskey=keywords.getKeyword(k);
      82      134176 :     if( keywords.style(thiskey,"flag") ) {
      83       61518 :       inputData.insert(std::pair<std::string,std::string>(thiskey,"false"));
      84             :     }
      85             :   }
      86             : 
      87             :   // Read command line arguments
      88             :   bool printhelp=false;
      89       15504 :   for(int i=1; i<argc; i++) {
      90       23394 :     a=prefix+argv[i];
      91       11697 :     if(a.length()==0) {
      92           0 :       continue;
      93             :     }
      94       23394 :     if(a=="-h" || a=="--help") {
      95             :       printhelp=true;
      96             :     } else {
      97             :       bool found=false;
      98      378100 :       for(unsigned k=0; k<keywords.size(); ++k) {
      99      366403 :         thiskey=keywords.getKeyword(k);
     100      732806 :         if( keywords.style(thiskey,"flag") ) {
     101       74889 :           if( a==thiskey ) {
     102             :             found=true;
     103        2884 :             inputData[thiskey]="true";
     104             :           }
     105             :         } else {
     106      291514 :           if( a==thiskey ) {
     107        4078 :             prefix=thiskey+"=";
     108             :             found=true;
     109        8156 :             inputData.insert(std::pair<std::string,std::string>(thiskey,""));
     110      574872 :           } else if(Tools::startWith(a,thiskey+"=")) {
     111        4735 :             a.erase(0,a.find("=")+1);
     112             :             prefix="";
     113             :             found=true;
     114             :             if(inputData.count(thiskey)==0) {
     115        1316 :               inputData.insert(std::pair<std::string,std::string>(thiskey,a));
     116             :             } else {
     117        4077 :               inputData[thiskey]=a;
     118             :             }
     119             :           }
     120             :         }
     121             :       }
     122       11697 :       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       11697 :     if(printhelp) {
     131             :       break;
     132             :     }
     133             :   }
     134             : 
     135        3807 :   if(!printhelp) {
     136        3807 :     setRemainingToDefault(out);
     137             :   }
     138             : 
     139        3807 :   if(printhelp) {
     140             :     std::fprintf(out,"Usage: %s [options] \n\n", name.c_str() );
     141           0 :     keywords.print( out );
     142             :   }
     143             : 
     144        3807 :   return !printhelp;
     145             : }
     146             : 
     147        3859 : void CLTool::setRemainingToDefault(FILE* out) {
     148             :   std::string def, thiskey;
     149       71955 :   for(unsigned k=0; k<keywords.size(); ++k) {
     150       68096 :     thiskey=keywords.getKeyword(k);
     151      136192 :     if( keywords.style(thiskey,"compulsory") ) {
     152             :       if( inputData.count(thiskey)==0 ) {
     153        2020 :         if( keywords.getDefaultValue(thiskey,def) ) {
     154        2020 :           plumed_assert( def.length()>0 );
     155        4040 :           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        3859 : }
     164             : 
     165          52 : bool CLTool::readInputFile( int argc, char**argv, FILE* in, FILE*out ) {
     166          52 :   plumed_assert(inputdata==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       16220 :     for(unsigned i=0; i<keywords.size(); ++i) {
     221       15446 :       std::string thiskey=keywords.getKeyword(i);
     222       15446 :       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             : }

Generated by: LCOV version 1.16