Loading...
Searching...
No Matches
CommandBankUtils.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_UTILS_H_
36#define COMMAND_BANK_UTILS_H_
37
38#include <iostream>
39#include <fstream>
40
41#include "CommandBank.h"
42#include "CommandSections.h"
43#include "drain/image/Image.h"
44
45
46namespace drain {
47
48
49// Static
50/*
51class CommandBankUtils : public CommandBank {
52public:
53};
54*/
55
56// Rename CommandUtils?
57
58
59
60
61class CmdVerbosity : public SimpleCommand<int> {
62
63public:
64
65 CmdVerbosity() : SimpleCommand<int>(__FUNCTION__, "Logging verbosity", "level", drain::getLog().getVerbosity()) {
66 };
67
68
69 inline
70 void exec() const {
71 // NEW
72 Context & ctx = getContext<Context>();
73 ctx.log.setVerbosity(value);
74
75 // OLD
76 drain::getLog().setVerbosity(value);
77 drain::image::getImgLog().setVerbosity(value-1);
78 }
79
80};
81
82class CmdDebug : public BasicCommand {
83
84public:
85
86 inline
87 CmdDebug() : BasicCommand(__FUNCTION__, "Set verbosity to LOG_DEBUG") { // TODO
88 };
89
90 inline
91 void exec() const {
92 Context & ctx = getContext<Context>();
93 ctx.log.setVerbosity(LOG_DEBUG);
94 drain::getLog().setVerbosity(LOG_DEBUG);
95 drain::image::getImgLog().setVerbosity(LOG_DEBUG);
96 //r.run("verbose","8"); // FIXME r.setVerbosity();
97 };
98
99};
100
101class CmdForEach : public drain::SimpleCommand<std::string> {
102
103public:
104
105 CmdForEach() : drain::SimpleCommand<std::string>(__FUNCTION__, "Iterate a list of values, calling a script for each", "key","val,val2,...","comma-separated list") {
106 };
107
108 void exec() const override;
109
110};
111
112/*
113class CmdForEach : public drain::SimpleCommand<std::string> {
114
115public:
116
117 CmdForEach() : drain::SimpleCommand<std::string>(__FUNCTION__, "Iterate values of status variable ${key}, calling a script for each", "key") {
118 };
119
120 void exec() const override;
121
122};
123*/
124//class CmdLog : public SimpleCommand<> {
125class CmdLog : public BasicCommand {
126
127public:
128
129 CmdLog(CommandBank & cmdBank);
130
131 CmdLog(const CmdLog & cmd);
132
133 void exec() const;
134
135protected:
136
137 CommandBank & bank;
138
139 std::string filename;
140
141 // For numeric or string keys
142 std::string level;
143
144 bool timing;
145
146};
147
148
149
150
151
153
154public:
155
156 inline
157 CmdStatus() : drain::BasicCommand(__FUNCTION__, "Dump information on current images.") {
158 };
159
160 void exec() const;
161
162};
163
164
171
172public:
173
174 inline
175 CmdExpandVariables() : BasicCommand(__FUNCTION__, "Toggle variable expansions on/off") {
176 };
177
178 inline
179 void exec() const {
180 SmartContext & ctx = getContext<SmartContext>();
181 ctx.expandVariables = !ctx.expandVariables;
182 };
183
184};
185
186
188
192class CmdScript : public SimpleCommand<std::string> {
193
194public:
195
196 inline
197 CmdScript(CommandBank & cmdBank) :
198 SimpleCommand<std::string>(__FUNCTION__, "Define script.", "script"),
199 bank(cmdBank){
200 cmdBank.scriptCmd = getName(); // mark me special
201 };
202
203 inline
204 CmdScript(const CmdScript & cmd) : SimpleCommand<std::string>(cmd), bank(cmd.bank){
205 }
206
207protected:
208
209 // Copy constructor should copy this as well.
210 // FUture versions may store the script in Context?
211 CommandBank & bank;
212
213};
214
216
220class CmdScriptFile : public SimpleCommand<std::string> {
221
222public:
223
224 inline
225 CmdScriptFile() : // CommandBank & cmdBank
226 SimpleCommand<std::string>(__FUNCTION__, "Define script.", "filename"){
227 //bank(cmdBank){
228 //cmdBank.scriptCmd = getName(); // mark me special
229 };
230
231 inline
233 }
234
235// protected:
236
237 //inline
238 void exec() const override;
239 /*
240 {
241 SmartContext & ctx = getContext<SmartContext>();
242 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
243 mout.debug("Storing script with '" , getName() , "' ." );
244 };
245 */
246
247
248};
249
250
252
259
260public:
261
262 inline
263 CmdExecScript() :
264 BasicCommand(__FUNCTION__, "Execute script.") {
265 // const drain::Flagger::ivalue_t
266 cmd_section_type TRIGGER = drain::Static::get<drain::TriggerSection>().index;
267 this->section |= TRIGGER;
268 };
269
270
271 inline
272 void exec() const {
273 SmartContext & ctx = getContext<SmartContext>();
274 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
275 mout.debug("Storing script with '" , getName() , "' ." );
276 };
277
278
279};
280
281
282
283
285
289class CmdExecFile : public SimpleCommand<std::string> {
290
291public:
292
293 CmdExecFile(CommandBank & cmdBank) :
294 SimpleCommand<std::string>(__FUNCTION__, "Execute commands from a file.", "filename"),
295 bank(cmdBank){
296 cmdBank.execFileCmd = getName(); // mark me special
297 };
298
299protected:
300
301 // Copy constructor should copy this as well.
302 CommandBank & bank;
303
304};
305
306
308class CmdHelp : public SimpleCommand<std::string> {
309
310public:
311
312
313 CmdHelp(CommandBank & cmdBank) : SimpleCommand<std::string>(__FUNCTION__, "Display help.", "key", "", "command|sections"), cmdBank(cmdBank) {
314 };
315
316 inline
317 void exec() const {
318 if (value.empty()){
319 if (cmdBank.has("general")){
320 cmdBank.help("general");
321 exit(0);
322 }
323 }
324 cmdBank.help(value);
325 // TODO: "see-also" commands as a list, which is checked.
326 exit(0);
327 }
328
329protected:
330
331 // Copy constructor should copy this as well.
332 CommandBank & cmdBank;
333
334};
335
336
337
338
339//template <class C=Context>
340class CmdFormat : public SimpleCommand<std::string> {
341
342public:
343
344 CmdFormat() : SimpleCommand<std::string>(__FUNCTION__,"Set format for data dumps (see --sample or --outputFile)", "format","") { // SimpleCommand<std::string>(getResources().generalCommands, name, alias, "Sets a format std::string.") {
345 };
346
347 inline
348 void exec() const {
349 SmartContext &ctx = getContext<SmartContext>();
350 ctx.formatStr = value;
351 }
352
353
354};
355
356
357
358
359template <class C=Context>
360class CmdFormatFile : public SimpleCommand<std::string> {
361
362public:
363
364
365 CmdFormatFile() : SimpleCommand<>(__FUNCTION__, "Read format for metadata dump from a file","filename","","std::string") {
366 };
367
368 void exec() const;
369
370};
371
372
373template <class C>
375
376 C &ctx = getContext<C>();
377
378 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
379
380 //drain::Input ifstr(value);
381 std::ifstream ifstr;
382 ifstr.open(value.c_str(), std::ios::in);
383 if (ifstr.good()){
384 std::stringstream sstr;
385 sstr << ifstr.rdbuf();
386 /*
387 for (int c = ifstr.get(); !ifstr.eof(); c = ifstr.get()){ // why not getline?
388 sstr << (char)c;
389 }
390 */
391 ifstr.close();
392 //Context &ctx = getContext<>();
393 ctx.formatStr = sstr.str(); // SmartContext ?
394
395 }
396 else
397 mout.error(getName() , ": opening file '" , value , "' failed." );
398
399}
400
401
403class CmdNotFound : public SimpleCommand<> {
404
405public:
406
407 CmdNotFound(CommandBank & cmdBank) :
408 SimpleCommand<std::string>(__FUNCTION__, "Throw exception on unfound ", "cmdArg","")
409 //cmdBank(cmdBank)
410 {
411 section = 0; // hidden
412 cmdBank.notFoundHandlerCmdKey = "notFound"; // getName();
413 };
414
415 void exec() const {
416 Context & ctx = getContext<>();
417
418 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
419
420 mout.error("Command '" , value , "' not found." );
422 }
423
424};
425
426
427
428} /* namespace drain */
429
430#endif
bool has(const K &key) const
Check if a cloner is defined for this key.
Definition Bank.h:140
Simple implementation of Command: adds name , description and parameters .
Definition Command.h:461
const std::string & getName() const final
Returns the command name.
Definition Command.h:384
Definition CommandBankUtils.h:82
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:91
Load script file and execute the commands immediately using current Context.
Definition CommandBankUtils.h:289
Executes the defined script.
Definition CommandBankUtils.h:258
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:272
Definition CommandBankUtils.h:170
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:179
Definition CommandBankUtils.h:101
void exec() const override
Run the command with current parameter values.
Definition CommandBankUtils.cpp:112
Definition CommandBankUtils.h:360
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:374
Definition CommandBankUtils.h:340
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:348
CommandBank-dependent.
Definition CommandBankUtils.h:308
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:317
Definition CommandBankUtils.h:125
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.cpp:52
Special command for handling undefined commands.
Definition CommandBankUtils.h:403
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:415
Define a script.
Definition CommandBankUtils.h:220
void exec() const override
Run the command with current parameter values.
Definition CommandBankUtils.cpp:148
Define a script.
Definition CommandBankUtils.h:192
Definition CommandBankUtils.h:152
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.cpp:73
Definition CommandBankUtils.h:61
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:70
Container and execution tools for commands derived from Command.
Definition CommandBank.h:67
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition CommandBank.h:278
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:321
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition CommandBank.cpp:949
std::string scriptCmd
Command for storing a routine.
Definition CommandBank.h:290
int cmd_section_type
Definition Command.h:187
cmd_section_type section
Section bitmask; zero means hidden from help.
Definition Command.h:188
std::string formatStr
Definition Context.h:214
Definition Context.h:55
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:676
Logger & error(const TT &... args)
Echoes.
Definition Log.h:426
A single-parameter command.
Definition Command.h:487
Definition Context.h:220
Definition Data.h:1473