JDFTx  1.2.1
command.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_COMMAND_COMMAND_H
21 #define JDFTX_COMMAND_COMMAND_H
22 
23 #include <electronic/common.h>
24 #include <commands/ParamList.h>
25 #include <core/string.h>
26 #include <memory>
27 #include <set>
28 
60 class Command
62 {
63 public:
64  string name;
65  string format;
66  string comments;
67  string section;
68  string category;
69  string subcategory;
70 
71  std::set<string> requires;
72  std::set<string> forbids;
73 
75  bool hasDefault;
76  string emptyParamError;
77 
82  virtual void process(ParamList& pl, Everything& e)=0;
83 
87  virtual void printStatus(Everything& e, int iRep)=0;
88 
89 protected:
97  Command(string name, string path);
98 
99  void require(string);
100  void forbid(string);
101 };
102 
105 {
106 public:
107  string name;
108  DeprecatedCommand(string name);
109 
110  //Return a replacement command and arguments pair
111  virtual std::pair<string,string> replace(ParamList& pl) const=0;
112 };
113 
114 std::map<string,Command*>& getCommandMap();
115 std::map<string,DeprecatedCommand*>& getDeprecatedMap();
116 
117 static EnumStringMap<bool> boolMap(false, "no", true, "yes");
118 
119 
126 template<typename GetDescription>
127 string addDescriptions(string optionList, const GetDescription& getDescription, string spacer="\n+ ")
128 {
129  //Determine max width of name, so as to align:
130  istringstream iss(optionList);
131  size_t nameWidth=0;
132  while(!iss.eof())
133  { string name;
134  getline(iss, name, '|');
135  trim(name);
136  nameWidth = std::max(name.length(),nameWidth);
137  }
138  //Process decsription list:
139  iss.seekg(0, std::ios::beg); //rewind;
140  string ret;
141  while(!iss.eof())
142  { //Get a name from the option list:
143  string name;
144  getline(iss, name, '|');
145  trim(name);
146  if(!name.length()) break;
147  //Get the description:
148  string desc = getDescription(name);
149  //Pad the name to required width:
150  if(name.length()<nameWidth)
151  name.resize(nameWidth, ' ');
152  ret += spacer + name;
153  if(desc.length()) ret += ": " + desc;
154  }
155  return ret;
156 }
157 
160 inline string nullDescription(const string&)
161 { return string();
162 }
163 
166 template<typename Enum> struct LinkDescription
167 { const EnumStringMap<Enum>& nameMap;
168  const EnumStringMap<Enum>& descMap;
169 
173  : nameMap(nameMap), descMap(descMap)
174  {
175  }
176 
180  string operator()(const string& name) const
181  { Enum type = Enum();
182  bool nameFound = nameMap.getEnum(name.c_str(), type);
183  assert(nameFound);
184  return descMap.getString(type);
185  }
186 };
187 
192 template<typename Enum>
194 { return LinkDescription<Enum>(nameMap, descMap);
195 }
196 
201 std::shared_ptr<SpeciesInfo> findSpecies(string id, Everything& e);
202 
209 bool isReadable(string fname);
210 
211 #endif // JDFTX_COMMAND_COMMAND_H
std::map< string, DeprecatedCommand * > & getDeprecatedMap()
Retrieve the map from deprecated command names to objects.
LinkDescription(const EnumStringMap< Enum > &nameMap, const EnumStringMap< Enum > &descMap)
Definition: command.h:172
STL strings and streams with case insensitive comparison.
string nullDescription(const string &)
Null description function that can be used with addDescriptions(). This can be useful for simply refo...
Definition: command.h:160
virtual void printStatus(Everything &e, int iRep)=0
Print a command line that would result in the current status.
A template to ease option parsing (maps enums <–> strings)
Definition: Util.h:179
string name
Identifier for the command in the input file. Must be unique!
Definition: command.h:64
string subcategory
Subcategory of command under which to list the documentation.
Definition: command.h:69
void require(string)
utility to add a command to the requires list
bool isReadable(string fname)
Check if file is readable in an MPI friendly way. This function must be called from all processes...
string emptyParamError
Error message if command is manually issued with no parameters. Ddefault: empty i.e. allow empty parameter list.
Definition: command.h:76
Generate a description functor for addDescriptions() from an EnumStringMap.
Definition: command.h:166
Base class for a deprecated command which will translate old syntax into the new command that replace...
Definition: command.h:104
Definition: string.h:77
bool allowMultiple
Whether this command can occur multiple times in an input file (default: false).
Definition: command.h:74
Helper class for parsing command lines in input file.
std::map< string, Command * > & getCommandMap()
Retrieve the map from command names to objects created during static initialization.
std::set< string > forbids
Names of other commands that this one is incompatibile with.
Definition: command.h:72
string comments
Detailed help for the command which goes into jdftx -t as well as the Doxygen manual. Please check formatting in both versions.
Definition: command.h:66
string category
Category of command under which to list the documentation.
Definition: command.h:68
Command(string name, string path)
void forbid(string)
utility to add a command to the forbids list
LinkDescription< Enum > linkDescription(const EnumStringMap< Enum > &nameMap, const EnumStringMap< Enum > &descMap)
Helper function to select appropriate LinkDescription<> by overloading.
Definition: command.h:193
string operator()(const string &name) const
Look name up in nameMap, and return the corresponding description in descMap.
Definition: command.h:180
void trim(string &s)
Remove leading and trailing spaces from a string.
Definition: string.h:45
Definition: Everything.h:41
string format
Usage syntax for the command (excluding the command name)
Definition: command.h:65
Wrapper to std::istringstream that eases parsing of input file command lines.
Definition: ParamList.h:30
Abstract base class for all commands.
Definition: command.h:61
std::set< string > requires
Names of other commands that this one requires; those commands will be processed before this one...
Definition: command.h:71
string addDescriptions(string optionList, const GetDescription &getDescription, string spacer="\n+ ")
Process the EnumStringMap::optionList() to add descriptions using an arbitrary functor.
Definition: command.h:127
string section
Which executable the command belongs to, and hence which section it must be documented under...
Definition: command.h:67
bool hasDefault
Whether this command has a default (default: false). If true, process() will be called with an empty ...
Definition: command.h:75
std::basic_string< char, ichar_traits > string
Case-insensitive string.
Definition: string.h:42
std::shared_ptr< SpeciesInfo > findSpecies(string id, Everything &e)
Find species matching an id (and create it from a wildcard if necessary)
#define assert(expr)
A custom assertion with stack trace (NOTE: enabled in release modes as well)
Definition: Util.h:100
virtual void process(ParamList &pl, Everything &e)=0
Process the command from its command line.