CommandBank.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 
32 
33 // New design (2020)
34 
35 #ifndef COMMAND_BANK_H_
36 #define COMMAND_BANK_H_
37 
38 #include <iostream>
39 
40 #include "drain/util/Bank.h"
41 #include "drain/util/Flags.h"
42 #include "Command.h"
43 #include "Program.h"
44 
45 
46 namespace drain {
47 
48 typedef int bank_section_t;
49 
51 
56 class CommandBank : public BankSuper<Command> { // BankSuper<BasicCommand> {
57 
58 public:
59 
60  typedef data_t command_t;
61 
63  };
64 
66  // Copy sections?
67  };
68 
69  //static // Otherwise ok, but handler as error...
70  //const Flagger::value_t SCRIPT_DEFINED;
71 
72 
74  static
75  std::set<std::string> & trimWords();
76 
78  static
79  void deriveCmdName(std::string & name, char prefix=0);
80  //const std::set<std::string> & trim = trimWords());
81 
82 
83  template <class D, char PREFIX=0>
84  D & addExternal(const D & entry, char alias = 0){
85  std::string key(entry.getName());
86  deriveCmdName(key, PREFIX);
87  //return BankSuper<Command>::template addExternal<D>(entry, key, alias);
88  return BankSuper<Command>::addExternal(entry, key, alias);
89  }
90 
91  // Template arg PREFIX is tricky, sometimes need for dynamic
92  template <class D>
93  D & addExternal(char PREFIX, const D & entry, char alias=0){
94  std::string key(entry.getName());
95  deriveCmdName(key, PREFIX);
96  return BankSuper<Command>::addExternal(entry, key, alias);
97  }
98 
99  template <class D, char PREFIX=0>
100  inline
101  D & addExternal(const D & entry, const std::string & key, char alias = 0){
102  //return BankSuper<Command>::template addExternal<D>(entry, key, alias);
103  return BankSuper<Command>::addExternal(entry, key, alias);
104  }
105 
106 
107 
109  //void includeFile(const std::string & filename, Program & prog, Program::iterator it) const;
110 
111 
113 
115 
118  void readFileTXT(const std::string & filename, Script & script) const;
119 
120  void readFile(const std::string & filename, Program & prog) const;
121 
123 
126  void scriptify(int argc, const char **argv, Script & script) const;
127 
129 
132  void scriptify(const std::string & cmdLine, Script & script) const;
133 
135 
140  bool scriptify(const std::string & arg, const std::string & argNext, Script & script) const;
141 
142 
144 
147  // void append(const Script & script, Program & prog, Context & context) const ;
148  void append(const Script & script, Program & prog) const ;
149 
150 protected:
151 
152  void append(const Script & script, Context & ctx, Program & prog) const ;
153 
154 
155  void tokenize(const std::string & cmdLine, std::list<std::string> & args) const;
156 
157 public:
158 
159  drain::StringMapper logFileSyntax; // todo:setter
160 
162  inline
163  void flushRoutine(Script & script, Context & ctx, Program & prog){
164  append(script, ctx, prog);
165  script.clear();
166  }
167 
168 
169  void remove(Program & prog) const;
170 
172  void run(const std::string & cmd, const std::string & params, Context & ctx);
173 
175  //void run(ScriptTxt & script, drain::Context & context);
176  // void run(Script & script, ClonerBase<Context> & contextSrc);
177 
178  // Newish
179  void run(Program & prog, ClonerBase<Context> & contextCloner); // !! May be RackContext!
180 
181 
183  void help(std::ostream & ostr = std::cout);
184 
186  void help(FlagResolver::ivalue_t sectionFilter, std::ostream & ostr = std::cout); // Flagger::ivalue_t sectionFilter
187 
189  void help(const std::string & key, std::ostream & ostr = std::cout);
190 
192  void info(const std::string & key, const command_t & cmd, std::ostream & ostr = std::cout, bool detailed=true) const ;
193 
195 
198  const std::string & resolveFull(const key_t & key) const;
199 
200  template <class T>
201  void exportStatus(std::map<std::string, T> & statusMap) const;
202 
203 
205  // drain::Flagger::ivalue_t
206  bank_section_t scriptTriggerFlag;
207 
208 
209  inline
210  void setScriptTriggerFlag(bank_section_t sectionFlag){ // drain::Flagger::ivalue_t
211  scriptTriggerFlag = sectionFlag;
212  };
213 
215 
219  std::string defaultCmdKey;
220 
222  inline
223  void setDefaultCmdKey(const std::string & s){
224  defaultCmdKey = s;
225  };
226 
229 
231 
234  inline
235  void setNotFoundHandlerCmdKey(const std::string & s){
237  };
238 
240  std::string scriptCmd;
241 
243  /***
244  * For example: "script"
245  */
246  inline
247  void setScriptCmd(const std::string & s){
248  scriptCmd = s;
249  }
250 
252  /***
253  * For example: "exec"
254  inline
255  void setScriptExecCmd(const std::string & s){
256  execScriptCmd = s;
257  }
258  */
259 
260 
262  /***
263  * For example: "execFile"
264  */
265  inline
266  void setScriptFileCmd(const std::string & s){
267  execFileCmd = s;
268  }
269 
271  std::string execFileCmd;
272 
273 
274 protected:
275 
277  // std::string execScriptCmd;
278 
279 
280 };
281 
282 template <class T>
283 void CommandBank::exportStatus(std::map<std::string, T> & statusMap) const {
284  for (map_t::const_iterator it = this->begin(); it!=this->end(); ++it){
285  statusMap[it->first] = it->second->getSource().getParameters().getValues();
286  }
287 }
288 
289 
291 extern
292 CommandBank & getCommandBank();
293 
294 
295 } /* namespace drain */
296 
297 #endif
A Bank with additional support for brief, single char keys.
Definition: Bank.h:314
Container and execution tools for commands derived from Command.
Definition: CommandBank.h:56
void scriptify(int argc, const char **argv, Script &script) const
Convert program arguments a script. Like in main(), actual command arguments start from 1.
Definition: CommandBank.cpp:180
void append(const Script &script, Program &prog) const
Converts command strings to executable command objects, appending them to a program.
Definition: CommandBank.cpp:140
void setNotFoundHandlerCmdKey(const std::string &s)
Set command to be executed when a command - argument with leading hyphen(s) - is not found.
Definition: CommandBank.h:235
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition: CommandBank.h:225
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition: CommandBank.h:271
static std::set< std::string > & trimWords()
Words that are moved from class name prior to composing a command name.
Definition: CommandBank.cpp:57
void flushRoutine(Script &script, Context &ctx, Program &prog)
Append routine to program, and clear routine.
Definition: CommandBank.h:163
bank_section_t scriptTriggerFlag
Member and methods for special commands.
Definition: CommandBank.h:206
const std::string & resolveFull(const key_t &key) const
Given an alias or a long key, possibly prefixed by hyphens, return the long key if a command exists.
Definition: CommandBank.cpp:1046
void run(const std::string &cmd, const std::string &params, Context &ctx)
Run a single command.
Definition: CommandBank.cpp:490
void readFile(const std::string &filename, Program &prog) const
Definition: CommandBank.cpp:383
std::string defaultCmdKey
Command that is used, if a plain argument is given.
Definition: CommandBank.h:212
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition: CommandBank.cpp:856
std::string scriptCmd
Command for storing a routine.
Definition: CommandBank.h:237
void info(const std::string &key, const command_t &cmd, std::ostream &ostr=std::cout, bool detailed=true) const
Checked key and respective command.
Definition: CommandBank.cpp:943
void readFileTXT(const std::string &filename, Script &script) const
Include commands from file, inserting them before iterator.
Definition: CommandBank.cpp:447
void setScriptFileCmd(const std::string &s)
Set command for reading and executing commands from a file in the current (running) context.
Definition: CommandBank.h:266
static void deriveCmdName(std::string &name, char prefix=0)
Given a command class name like MyFileReadCommand, derives a respective command line option ("myFileR...
Definition: CommandBank.cpp:62
void setScriptCmd(const std::string &s)
Set command for storing a routine.
Definition: CommandBank.h:247
void setDefaultCmdKey(const std::string &s)
Set command - key, not getName() - that is used if a plain argument is given.
Definition: CommandBank.h:223
Definition: Context.h:57
Sequence consisting of Command:s retrieved from a CommandBank.
Definition: Program.h:101
Definition: Program.h:85
A tool for expanding variables embedded in a std::string to literals.
Definition: StringMapper.h:275
Definition: DataSelector.cpp:1277
CommandBank & getCommandBank()
Global program command registry. Optional utility.
Definition: CommandBank.cpp:51
Definition: Cloner.h:46