-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathCliCommand.h
132 lines (114 loc) · 4.63 KB
/
CliCommand.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//==============================================================================
//
// CliCommand.h
//
// Copyright (C) 2013-2025 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the Lesser GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the Lesser GNU General Public License
// along with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef CLICOMMAND_H_INCLUDED
#define CLICOMMAND_H_INCLUDED
#include "CliText.h"
#include <cstdint>
#include <iosfwd>
#include "SysTypes.h"
//------------------------------------------------------------------------------
namespace NodeBase
{
// CLI command.
//
class CliCommand : public CliText
{
friend class CliThread;
public:
//> Field width for command names in help text.
//
static const int CommandWidth;
// Separates the name of an increment from a command in help text.
//
static const char CommandSeparator;
// Used in software logs when a command doesn't recognize its first
// parameter.
//
static fixed_string UnexpectedIndex;
// Virtual to allow subclassing.
//
virtual ~CliCommand();
// Explains the command. If VERBOSE is true, all of the command's
// parameters are also explained. Returns 0.
//
virtual word ExplainCommand(std::ostream& stream, bool verbose) const;
// Overridden to look for a boolean as the next parameter.
//
Rc GetBoolParmRc(bool& b, CliThread& cli) const override;
// Overridden to look for a character as the next parameter.
//
Rc GetCharParmRc(char& c, CliThread& cli) const override;
// Overridden to look for a filename as the next parameter.
//
Rc GetFileNameRc(std::string& s, CliThread& cli) const override;
// Overridden to look for an identifier as the next parameter.
//
Rc GetIdentifierRc(std::string& s, CliThread& cli,
const std::string& valid, const std::string& exclude) const override;
// Overridden to look for an integer as the next parameter.
//
Rc GetIntParmRc(word& n, CliThread& cli) const override;
// Overridden to look for a pointer as the next parameter.
//
Rc GetPtrParmRc(void*& p, CliThread& cli) const override;
// Overridden to look for an arbitrary string as the next parameter.
//
Rc GetStringRc(std::string& s, CliThread& cli) const override;
// Overridden to look for a string as the next parameter.
//
Rc GetTextParmRc(id_t& i, std::string& s, CliThread& cli) const override;
// Overridden for patching.
//
void Patch(sel_t selector, void* arguments) override;
protected:
// HELP, COMM (the command's name), and SIZE (the maximum number
// of parameters that follow the command) are passed to CliText.
// Protected because this is class is virtual.
//
CliCommand(c_string comm, c_string help, uint32_t size = 32);
// This version of ProcessCommand allows a command to appear in more
// than one module. The command's first parameter is a CliTextParm
// that registers "subcommand" strings at fixed indices. In the base
// class, the basic version of ProcessCommand (above) parses the index
// and then invokes this version of ProcessCommand, passing the index
// as an argument. A subclass overrides this version to implement its
// extensions to the base class, invoking the base class when INDEX is
// outside its range. The default version generates a log and returns
// -1, and can be invoked to report an error if INDEX is out of bounds.
//
virtual word ProcessSubcommand(CliThread& cli, id_t index) const;
private:
// The function that actually implements the command. It is invoked
// when the command is entered through the CLI. If the command takes
// parameters, it calls a series of "Get" functions to obtain them.
// The command then performs its work. Return values are command
// specific, but a zero or positive value generally denotes success,
// with negative values denoting errors or warnings.
//
virtual word ProcessCommand(CliThread& cli) const = 0;
// Overridden to stop looking for parameters if those below this
// command have been exhausted.
//
bool Ascend() const override { return false; }
};
}
#endif