Loading...
Searching...
No Matches
CommandBank.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood 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
46namespace drain {
47
48typedef int bank_section_t;
49
51
56class CommandBank : public BankSuper<Command> { // BankSuper<BasicCommand> {
57
58public:
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
112
114
117 // static
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 //static
133 void scriptify(const std::string & cmdLine, Script & script) const;
134
136
141 bool scriptify(const std::string & arg, const std::string & argNext, Script & script) const;
142
143
145
148 // void append(const Script & script, Program & prog, Context & context) const ;
149 void append(const Script & script, Program & prog) const ;
150
151protected:
152
153 void append(const Script & script, Context & ctx, Program & prog) const ;
154
155
156 static
157 void tokenize(const std::string & cmdLine, std::list<std::string> & args);
158
159public:
160
161 drain::StringMapper logFileSyntax; // todo:setter
162
164 inline
165 void flushRoutine(Script & script, Context & ctx, Program & prog){
166 append(script, ctx, prog);
167 script.clear();
168 }
169
170
171 void remove(Program & prog) const;
172
174 void run(const std::string & cmd, const std::string & params, Context & ctx);
175
177 //void run(ScriptTxt & script, drain::Context & context);
178 // void run(Script & script, ClonerBase<Context> & contextSrc);
179
180 // Newish
181 void run(Program & prog, ClonerBase<Context> & contextCloner); // !! May be RackContext!
182
183
185 void help(std::ostream & ostr = std::cout);
186
188 void help(FlagResolver::ivalue_t sectionFilter, std::ostream & ostr = std::cout); // Flagger::ivalue_t sectionFilter
189
191 void help(const std::string & key, std::ostream & ostr = std::cout);
192
194 void info(const std::string & key, const command_t & cmd, std::ostream & ostr = std::cout, bool detailed=true) const ;
195
197
200 const std::string & resolveFull(const key_t & key) const;
201
202 template <class T>
203 void exportStatus(std::map<std::string, T> & statusMap) const;
204
205
207 // drain::Flagger::ivalue_t
208 bank_section_t scriptTriggerFlag;
209
210
211 inline
212 void setScriptTriggerFlag(bank_section_t sectionFlag){ // drain::Flagger::ivalue_t
213 scriptTriggerFlag = sectionFlag;
214 };
215
217
221 std::string defaultCmdKey;
222
224 inline
225 void setDefaultCmdKey(const std::string & s){
226 defaultCmdKey = s;
227 };
228
231
233
236 inline
237 void setNotFoundHandlerCmdKey(const std::string & s){
239 };
240
242 std::string scriptCmd;
243
245 /***
246 * For example: "script"
247 */
248 inline
249 void setScriptCmd(const std::string & s){
250 scriptCmd = s;
251 }
252
254 /***
255 * For example: "exec"
256 inline
257 void setScriptExecCmd(const std::string & s){
258 execScriptCmd = s;
259 }
260 */
261
262
264 /***
265 * For example: "execFile"
266 */
267 inline
268 void setScriptFileCmd(const std::string & s){
269 execFileCmd = s;
270 }
271
273 std::string execFileCmd;
274
275
276 template <class ...TT>
277 void linkRelatedCommands(const TT & ... cmds){
278 std::set<std::string> cmdList;
279 linkRelatedCommandList(cmdList, cmds...);
280 }
281
283 void linkRelatedCommandList(const std::set<std::string> & cmdList);
284
285
286protected:
287
288 template <class ...TT>
289 void linkRelatedCommandList(std::set<std::string> & cmdList, const std::string & cmd, const TT & ... cmds){
290 cmdList.insert(cmd);
291 linkRelatedCommandList(cmdList, cmds...);
292 }
293
294
295 template <class ...TT>
296 void linkRelatedCommandList(std::set<std::string> & cmdList, const char *cmd, const TT & ... cmds){
297 cmdList.insert(cmd);
298 linkRelatedCommandList(cmdList, cmds...);
299 }
300
301
302};
303
304template <class T>
305void CommandBank::exportStatus(std::map<std::string, T> & statusMap) const {
306 for (map_t::const_iterator it = this->begin(); it!=this->end(); ++it){
307 statusMap[it->first] = it->second->getSource().getParameters().getValues();
308 }
309}
310
311
313extern
314CommandBank & getCommandBank();
315
316
317} /* namespace drain */
318
319#endif
A Bank with additional support for brief, single char keys.
Definition Bank.h:317
Container and execution tools for commands derived from Command.
Definition CommandBank.h:56
void linkRelatedCommandList(const std::set< std::string > &cmdList)
Adds command keys, intelinking all the commands in the list.
Definition CommandBank.cpp:917
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:237
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition CommandBank.h:230
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:273
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:165
bank_section_t scriptTriggerFlag
Member and methods for special commands.
Definition CommandBank.h:208
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:1061
void run(const std::string &cmd, const std::string &params, Context &ctx)
Run a single command.
Definition CommandBank.cpp:466
void readFile(const std::string &filename, Program &prog) const
Definition CommandBank.cpp:356
std::string defaultCmdKey
Command that is used, if a plain argument is given.
Definition CommandBank.h:221
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition CommandBank.cpp:832
std::string scriptCmd
Command for storing a routine.
Definition CommandBank.h:242
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:948
void readFileTXT(const std::string &filename, Script &script) const
Include commands from file, inserting them before iterator.
Definition CommandBank.cpp:420
void setScriptFileCmd(const std::string &s)
Set command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:268
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:249
void setDefaultCmdKey(const std::string &s)
Set command - key, not getName() - that is used if a plain argument is given.
Definition CommandBank.h:225
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