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 
49 
54 class CommandBank : public BankSuper<Command> { // BankSuper<BasicCommand> {
55 
56 public:
57 
58  typedef data_t command_t;
59 
61  };
62 
64  // Copy sections?
65  };
66 
67  //static // Otherwise ok, but handler as error...
68  //const Flagger::value_t SCRIPT_DEFINED;
69 
70 
72  static
73  std::set<std::string> & trimWords();
74 
76  static
77  void deriveCmdName(std::string & name, char prefix=0);
78  //const std::set<std::string> & trim = trimWords());
79 
80 
81  template <class D, char PREFIX=0>
82  D & addExternal(const D & entry, char alias = 0){
83  std::string key(entry.getName());
84  deriveCmdName(key, PREFIX);
85  //return BankSuper<Command>::template addExternal<D>(entry, key, alias);
86  return BankSuper<Command>::addExternal(entry, key, alias);
87  }
88 
89  // Template arg PREFIX is tricky, sometimes need for dynamic
90  template <class D>
91  D & addExternal(char PREFIX, const D & entry, char alias=0){
92  std::string key(entry.getName());
93  deriveCmdName(key, PREFIX);
94  return BankSuper<Command>::addExternal(entry, key, alias);
95  }
96 
97  template <class D, char PREFIX=0>
98  inline
99  D & addExternal(const D & entry, const std::string & key, char alias = 0){
100  //return BankSuper<Command>::template addExternal<D>(entry, key, alias);
101  return BankSuper<Command>::addExternal(entry, key, alias);
102  }
103 
104 
105 
107  //void includeFile(const std::string & filename, Program & prog, Program::iterator it) const;
108 
109 
111 
113 
116  void readFileTXT(const std::string & filename, Script & script) const;
117 
118  void readFile(const std::string & filename, Program & prog) const;
119 
121 
124  void scriptify(int argc, const char **argv, Script & script) const;
125 
127 
130  void scriptify(const std::string & cmdLine, Script & script) const;
131 
133 
138  bool scriptify(const std::string & arg, const std::string & argNext, Script & script) const;
139 
140 
142 
145  // void append(const Script & script, Program & prog, Context & context) const ;
146  void append(const Script & script, Program & prog) const ;
147 
148 protected:
149 
150  void append(const Script & script, Context & ctx, Program & prog) const ;
151 
152 
153  void tokenize(const std::string & cmdLine, std::list<std::string> & args) const;
154 
155 public:
156 
157  drain::StringMapper logFileSyntax; // todo:setter
158 
160  inline
161  void flushRoutine(Script & script, Context & ctx, Program & prog){
162  append(script, ctx, prog);
163  script.clear();
164  }
165 
166 
167  void remove(Program & prog) const;
168 
170  void run(const std::string & cmd, const std::string & params, Context & ctx);
171 
173  //void run(ScriptTxt & script, drain::Context & context);
174  // void run(Script & script, ClonerBase<Context> & contextSrc);
175 
176  // Newish
177  void run(Program & prog, ClonerBase<Context> & contextCloner); // !! May be RackContext!
178 
179 
181  void help(std::ostream & ostr = std::cout);
182 
184  void help(Flagger::ivalue_t sectionFilter, std::ostream & ostr = std::cout);
185 
187  void help(const std::string & key, std::ostream & ostr = std::cout);
188 
190  void info(const std::string & key, const command_t & cmd, std::ostream & ostr = std::cout, bool detailed=true) const ;
191 
193 
196  const std::string & resolveFull(const key_t & key) const;
197 
198  template <class T>
199  void exportStatus(std::map<std::string, T> & statusMap) const;
200 
201 
203  drain::Flagger::ivalue_t scriptTriggerFlag;
204 
205 
206  inline
207  void setScriptTriggerFlag(drain::Flagger::ivalue_t sectionFlag){
208  scriptTriggerFlag = sectionFlag;
209  };
210 
212 
216  std::string defaultCmdKey;
217 
219  inline
220  void setDefaultCmdKey(const std::string & s){
221  defaultCmdKey = s;
222  };
223 
226 
228 
231  inline
232  void setNotFoundHandlerCmdKey(const std::string & s){
234  };
235 
237  std::string scriptCmd;
238 
240  /***
241  * For example: "script"
242  */
243  inline
244  void setScriptCmd(const std::string & s){
245  scriptCmd = s;
246  }
247 
249  /***
250  * For example: "exec"
251  inline
252  void setScriptExecCmd(const std::string & s){
253  execScriptCmd = s;
254  }
255  */
256 
257 
259  /***
260  * For example: "execFile"
261  */
262  inline
263  void setScriptFileCmd(const std::string & s){
264  execFileCmd = s;
265  }
266 
268  std::string execFileCmd;
269 
270 
271 protected:
272 
274  // std::string execScriptCmd;
275 
276 
277 };
278 
279 template <class T>
280 void CommandBank::exportStatus(std::map<std::string, T> & statusMap) const {
281  for (map_t::const_iterator it = this->begin(); it!=this->end(); ++it){
282  statusMap[it->first] = it->second->getSource().getParameters().getValues();
283  }
284 }
285 
286 
288 extern
289 CommandBank & getCommandBank();
290 
291 
292 } /* namespace drain */
293 
294 #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:54
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:232
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition: CommandBank.h:222
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition: CommandBank.h:268
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:161
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:1040
void run(const std::string &cmd, const std::string &params, Context &ctx)
Run a single command.
Definition: CommandBank.cpp:484
std::string defaultCmdKey
Command that is used, if a plain argument is given.
Definition: CommandBank.h:209
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition: CommandBank.cpp:850
std::string scriptCmd
Command for storing a routine.
Definition: CommandBank.h:234
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:937
void readFileTXT(const std::string &filename, Script &script) const
Include commands from file, inserting them before iterator.
Definition: CommandBank.cpp:441
void setScriptFileCmd(const std::string &s)
Set command for reading and executing commands from a file in the current (running) context.
Definition: CommandBank.h:263
drain::Flagger::ivalue_t scriptTriggerFlag
Member and methods for special commands.
Definition: CommandBank.h:203
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:244
void setDefaultCmdKey(const std::string &s)
Set command - key, not getName() - that is used if a plain argument is given.
Definition: CommandBank.h:220
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