AceUtils  0.6.0
Useful Arduino utilties which are too small as separate libraries, but complex enough to be shared among multiple projects, and often have external dependencies to other libraries.
CommandDispatcher.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_UTILS_CLI_COMMAND_DISPATCHER_H
26 #define ACE_UTILS_CLI_COMMAND_DISPATCHER_H
27 
28 #include "CommandHandler.h"
29 
30 class Print;
31 class __FlashStringHelper;
32 
33 namespace ace_utils {
34 namespace cli {
35 
51  public:
63  const CommandHandler* const* commands,
64  uint8_t numCommands,
65  const char** argv,
66  uint8_t argvSize
67  ) :
68  mCommands(commands),
69  mNumCommands(numCommands),
70  mArgv(argv),
71  mArgvSize(argvSize)
72  {}
73 
78  void runCommand(Print& printer, char* line) const;
79 
88  static uint8_t tokenize(char* line, const char** argv, uint8_t argvSize) {
89  char* token = strtok(line, DELIMS);
90  int argc = 0;
91  while (token != nullptr && argc < argvSize) {
92  argv[argc] = token;
93  argc++;
94  token = strtok(nullptr, DELIMS);
95  }
96  return argc;
97  }
98 
108  const CommandHandler* findCommand(const char* cmd) const {
109  for (uint8_t i = 0; i < mNumCommands; i++) {
110  const CommandHandler* command = mCommands[i];
111  if (command->getName().compareTo(ace_common::FCString(cmd)) == 0) {
112  return command;
113  }
114  }
115  return nullptr;
116  }
117 
118  private:
119  // Disable copy-constructor and assignment operator
120  CommandDispatcher(const CommandDispatcher&) = delete;
121  CommandDispatcher& operator=(const CommandDispatcher&) = delete;
122 
124  void helpCommandHandler(Print& printer, int argc, const char* const* argv)
125  const;
126 
128  void helpAll(Print& printer) const;
129 
131  bool helpSpecific(Print& printer, const char* cmd) const;
132 
134  static void printHelp(Print& printer, const CommandHandler* command);
135 
137  void findAndRunCommand(Print& printer, const char* cmd,
138  int argc, const char* const* argv) const;
139 
140  private:
141  static const char DELIMS[];
142 
143  const CommandHandler* const* const mCommands;
144  uint8_t const mNumCommands;
145  const char** const mArgv;
146  uint8_t const mArgvSize;
147 };
148 
149 } // cli
150 } // ace_utils
151 
152 #endif
A class that tokenizes a line containing tokens separated on whitespace boundaries,...
CommandDispatcher(const CommandHandler *const *commands, uint8_t numCommands, const char **argv, uint8_t argvSize)
Constructor.
static uint8_t tokenize(char *line, const char **argv, uint8_t argvSize)
Tokenize the line, separating tokens delimited by whitespace (space, formfeed, carriage return,...
const CommandHandler * findCommand(const char *cmd) const
Find the CommandHandler of the given command name.
void runCommand(Print &printer, char *line) const
Tokenize the given 'line', run the matching command handler, and send output to the 'printer'.
Signature for a command handler.
ace_common::FCString getName() const
Return the name of the command.