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 CmdFor : public drain::SimpleCommand<std::string> {
102
103public:
104
105 CmdFor() : 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
215
217
224
225public:
226
227 inline
228 CmdExecScript() :
229 BasicCommand(__FUNCTION__, "Execute script.") {
230 // const drain::Flagger::ivalue_t
231 cmd_section_type TRIGGER = drain::Static::get<drain::TriggerSection>().index;
232 this->section |= TRIGGER;
233 };
234
235
236 inline
237 void exec() const {
238 SmartContext & ctx = getContext<SmartContext>();
239 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
240 mout.debug("Storing script with '" , getName() , "' ." );
241 };
242
243
244};
245
246
247
248
250
254class CmdExecFile : public SimpleCommand<std::string> {
255
256public:
257
258 CmdExecFile(CommandBank & cmdBank) :
259 SimpleCommand<std::string>(__FUNCTION__, "Execute commands from a file.", "filename"),
260 bank(cmdBank){
261 cmdBank.execFileCmd = getName(); // mark me special
262 };
263
264protected:
265
266 // Copy constructor should copy this as well.
267 CommandBank & bank;
268
269};
270
271
273class CmdHelp : public SimpleCommand<std::string> {
274
275public:
276
277
278 CmdHelp(CommandBank & cmdBank) : SimpleCommand<std::string>(__FUNCTION__, "Display help.", "key", "", "command|sections"), cmdBank(cmdBank) {
279 };
280
281 inline
282 void exec() const {
283 if (value.empty()){
284 if (cmdBank.has("general")){
285 cmdBank.help("general");
286 exit(0);
287 }
288 }
289 cmdBank.help(value);
290 // TODO: "see-also" commands as a list, which is checked.
291 exit(0);
292 }
293
294protected:
295
296 // Copy constructor should copy this as well.
297 CommandBank & cmdBank;
298
299};
300
301
302
303
304//template <class C=Context>
305class CmdFormat : public SimpleCommand<std::string> {
306
307public:
308
309 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.") {
310 };
311
312 inline
313 void exec() const {
314 SmartContext &ctx = getContext<SmartContext>();
315 ctx.formatStr = value;
316 }
317
318
319};
320
321
322
323
324template <class C=Context>
325class CmdFormatFile : public SimpleCommand<std::string> {
326
327public:
328
329
330 CmdFormatFile() : SimpleCommand<>(__FUNCTION__, "Read format for metadata dump from a file","filename","","std::string") {
331 };
332
333 void exec() const;
334
335};
336
337
338template <class C>
340
341 C &ctx = getContext<C>();
342
343 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
344
345 //drain::Input ifstr(value);
346 std::ifstream ifstr;
347 ifstr.open(value.c_str(), std::ios::in);
348 if (ifstr.good()){
349 std::stringstream sstr;
350 sstr << ifstr.rdbuf();
351 /*
352 for (int c = ifstr.get(); !ifstr.eof(); c = ifstr.get()){ // why not getline?
353 sstr << (char)c;
354 }
355 */
356 ifstr.close();
357 //Context &ctx = getContext<>();
358 ctx.formatStr = sstr.str(); // SmartContext ?
359
360 }
361 else
362 mout.error(getName() , ": opening file '" , value , "' failed." );
363
364};
365
366
368class CmdNotFound : public SimpleCommand<> {
369
370public:
371
372 CmdNotFound(CommandBank & cmdBank) :
373 SimpleCommand<std::string>(__FUNCTION__, "Throw exception on unfound ", "cmdArg","")
374 //cmdBank(cmdBank)
375 {
376 section = 0; // hidden
377 cmdBank.notFoundHandlerCmdKey = "notFound"; // getName();
378 };
379
380 void exec() const {
381 Context & ctx = getContext<>();
382
383 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
384
385 mout.error("Command '" , value , "' not found." );
387 }
388
389};
390
391
392
393} /* namespace drain */
394
395#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:443
const std::string & getName() const final
Definition Command.h:368
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:254
Executes the defined script.
Definition CommandBankUtils.h:223
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:237
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:325
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:339
Definition CommandBankUtils.h:305
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:313
CommandBank-dependent.
Definition CommandBankUtils.h:273
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:282
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:368
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:380
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:56
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition CommandBank.h:242
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:285
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:254
int cmd_section_type
Definition Command.h:180
std::string formatStr
Definition Context.h:211
Definition Context.h:55
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:667
Logger & error(const TT &... args)
Echoes.
Definition Log.h:417
A single-parameter command.
Definition Command.h:467
Definition Context.h:217
Definition DataSelector.cpp:1277