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
101//class CmdLog : public SimpleCommand<> {
102class CmdLog : public BasicCommand {
103
104public:
105
106 CmdLog(CommandBank & cmdBank);
107
108 CmdLog(const CmdLog & cmd);
109
110 void exec() const;
111
112protected:
113
114 CommandBank & bank;
115
116 std::string filename;
117
118 // For numeric or string keys
119 std::string level;
120
121 bool timing;
122
123};
124
125
126
127
128
130
131public:
132
133 inline
134 CmdStatus() : drain::BasicCommand(__FUNCTION__, "Dump information on current images.") {
135 };
136
137 void exec() const;
138
139};
140
141
143
144public:
145
146 inline
147 CmdExpandVariables() : BasicCommand(__FUNCTION__, "Toggle variable expansions on/off") {
148 };
149
150 inline
151 void exec() const {
152 SmartContext & ctx = getContext<SmartContext>();
153 //CommandRegistry & r = getRegistry();
154 ctx.expandVariables = !ctx.expandVariables;
155 };
156
157};
158
159
161
165class CmdScript : public SimpleCommand<std::string> {
166
167public:
168
169 inline
170 CmdScript(CommandBank & cmdBank) :
171 SimpleCommand<std::string>(__FUNCTION__, "Define script.", "script"),
172 bank(cmdBank){
173 cmdBank.scriptCmd = getName(); // mark me special
174 };
175
176 inline
177 CmdScript(const CmdScript & cmd) : SimpleCommand<std::string>(cmd), bank(cmd.bank){
178 }
179
180protected:
181
182 // Copy constructor should copy this as well.
183 // FUture versions may store the script in Context?
184 CommandBank & bank;
185
186};
187
188
190
195
196public:
197
198 inline
199 CmdExecScript() :
200 BasicCommand(__FUNCTION__, "Execute script.") {
201 // const drain::Flagger::ivalue_t
202 cmd_section_type TRIGGER = drain::Static::get<drain::TriggerSection>().index;
203 this->section |= TRIGGER;
204 /*
205 drain::Logger mout(__FILE__, __FUNCTION__);
206 mout.experimental("constr: TRIGGER=" , TRIGGER );
207 mout.experimental("My sections: " , this->section );
208 */
209 };
210
211 /*
212 inline
213 CmdExecScript(const CmdExecScript & cmd) : BasicCommand(cmd){
214 drain::Logger mout(__FILE__, __FUNCTION__);
215 mout.experimental("Copy constr, their sections: " , cmd.section );
216 mout.experimental("Copy constr, my sections: " , this->section );
217 };
218 */
219 /*
220 CmdExecScript(CommandBank & cmdBank) :
221 BasicCommand(__FUNCTION__, "Execute script.") {
222 //cmdBank.setScriptExecCmd(getName());
223 const drain::Flagger::value_t TRIGGER = drain::Static::get<drain::TriggerSection>().index;
224 cmdBank.setScriptTriggerFlag(TRIGGER);
225 this->section |= TRIGGER;
226
227 drain::Logger mout(__FILE__, __FUNCTION__);
228 mout.experimental("constr: TRIGGER=" , TRIGGER );
229 mout.experimental(*this );
230 mout.experimental("My sections: '" , this->section , "' ." );
231
232 };
233 */
234
235 inline
236 void exec() const {
237 SmartContext & ctx = getContext<SmartContext>();
238 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
239 mout.debug("Executing script with '" , getName() , "' ." );
240 //mout.warn("My sections: '" , this->section , "' ." );
241 //mout.error("This command '" , getName() , "' cannot be run independently." );
242 };
243
244 // reconsider exec();
245
246protected:
247
248 // Copy constructor should copy this as well.
249 // FUture versions may store the script in Context!
250 //CommandBank & bank;
251
252};
253
254
255
257
261class CmdExecFile : public SimpleCommand<std::string> {
262
263public:
264
265 CmdExecFile(CommandBank & cmdBank) :
266 SimpleCommand<std::string>(__FUNCTION__, "Execute commands from a file.", "filename"),
267 bank(cmdBank){
268 cmdBank.execFileCmd = getName(); // mark me special
269 };
270
271protected:
272
273 // Copy constructor should copy this as well.
274 CommandBank & bank;
275
276};
277
278
280class HelpCmd : public SimpleCommand<std::string> {
281
282public:
283
284
285 HelpCmd(CommandBank & cmdBank) : SimpleCommand<std::string>(__FUNCTION__, "Display help.", "key", "", "command|sections"), cmdBank(cmdBank) {
286 };
287
288 inline
289 void exec() const {
290 if (value.empty()){
291 if (cmdBank.has("general")){
292 cmdBank.help("general");
293 exit(0);
294 }
295 }
296 cmdBank.help(value);
297 // TODO: "see-also" commands as a list, which is checked.
298 exit(0);
299 }
300
301protected:
302
303 // Copy constructor should copy this as well.
304 CommandBank & cmdBank;
305
306};
307
308
309
310
311//template <class C=Context>
312class CmdFormat : public SimpleCommand<std::string> {
313
314public:
315
316 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.") {
317 };
318
319 inline
320 void exec() const {
321 SmartContext &ctx = getContext<SmartContext>();
322 ctx.formatStr = value;
323 }
324
325
326};
327
328
329
330
331template <class C=Context>
332class CmdFormatFile : public SimpleCommand<std::string> {
333
334public:
335
336
337 CmdFormatFile() : SimpleCommand<>(__FUNCTION__, "Read format for metadata dump from a file","filename","","std::string") {
338 };
339
340 void exec() const;
341
342};
343
344
345template <class C>
347
348 C &ctx = getContext<C>();
349
350 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
351
352 //drain::Input ifstr(value);
353 std::ifstream ifstr;
354 ifstr.open(value.c_str(), std::ios::in);
355 if (ifstr.good()){
356 std::stringstream sstr;
357 sstr << ifstr.rdbuf();
358 /*
359 for (int c = ifstr.get(); !ifstr.eof(); c = ifstr.get()){ // why not getline?
360 sstr << (char)c;
361 }
362 */
363 ifstr.close();
364 //Context &ctx = getContext<>();
365 ctx.formatStr = sstr.str(); // SmartContext ?
366
367 }
368 else
369 mout.error(getName() , ": opening file '" , value , "' failed." );
370
371};
372
373
375class CmdNotFound : public SimpleCommand<> {
376
377public:
378
379 CmdNotFound(CommandBank & cmdBank) :
380 SimpleCommand<std::string>(__FUNCTION__, "Throw exception on unfound ", "cmdArg","")
381 //cmdBank(cmdBank)
382 {
383 section = 0; // hidden
384 cmdBank.notFoundHandlerCmdKey = "notFound"; // getName();
385 };
386
387 void exec() const {
388 Context & ctx = getContext<>();
389
390 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
391
392 mout.error("Command '" , value , "' not found." );
394 }
395
396};
397
398
399
400} /* namespace drain */
401
402#endif
bool has(const K &key) const
Check if a cloner is defined for this key.
Definition Bank.h:141
Simple implementation of Command: adds name , description and parameters .
Definition Command.h:424
const std::string & getName() const final
Definition Command.h:349
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:261
Executes the defined script.
Definition CommandBankUtils.h:194
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:236
Definition CommandBankUtils.h:142
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:151
Definition CommandBankUtils.h:332
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:346
Definition CommandBankUtils.h:312
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:320
Definition CommandBankUtils.h:102
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.cpp:52
Special command for handling undefined commands.
Definition CommandBankUtils.h:375
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:387
Load script file and executes the commands immediately.
Definition CommandBankUtils.h:165
Definition CommandBankUtils.h:129
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:230
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:273
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition CommandBank.cpp:860
std::string scriptCmd
Command for storing a routine.
Definition CommandBank.h:242
int cmd_section_type
Definition Command.h:184
std::string formatStr
Definition Context.h:199
Definition Context.h:57
CommandBank-dependent.
Definition CommandBankUtils.h:280
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:289
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:666
Logger & error(const TT &... args)
Echoes.
Definition Log.h:416
A single-parameter command.
Definition Command.h:448
Definition Context.h:205
Definition DataSelector.cpp:1277