AceUtils
0.5.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.
|
25 #ifndef ACE_UTILS_CLI_COMMAND_DISPATCHER_H
26 #define ACE_UTILS_CLI_COMMAND_DISPATCHER_H
28 #include <AceRoutine.h>
29 #include "CommandHandler.h"
30 #include "InputLine.h"
33 class __FlashStringHelper;
69 ace_routine::Channel<InputLine>& channel,
79 mNumCommands(numCommands),
95 static uint8_t
tokenize(
char* line,
const char** argv, uint8_t argvSize) {
96 char* token = strtok(line, DELIMS);
98 while (token !=
nullptr && argc < argvSize) {
101 token = strtok(
nullptr, DELIMS);
106 int runCoroutine()
override {
109 if (mPrompt !=
nullptr) {
110 mPrinter.print(mPrompt);
112 COROUTINE_CHANNEL_READ(mChannel, input);
114 if (input.status == InputLine::kStatusOverflow) {
119 if (mPrompt !=
nullptr) {
120 mPrinter.print(input.line);
136 for (uint8_t i = 0; i < mNumCommands; i++) {
138 if (command->
getName().compareTo(ace_common::FCString(cmd)) == 0) {
158 void helpAll(Print& printer)
const;
161 bool helpSpecific(Print& printer,
const char* cmd)
const;
173 static const uint8_t STATUS_SUCCESS = 0;
174 static const uint8_t STATUS_BUFFER_OVERFLOW = 1;
175 static const uint8_t STATUS_FLUSH_TO_EOL = 2;
176 static const char DELIMS[];
178 ace_routine::Channel<InputLine>& mChannel;
181 uint8_t
const mNumCommands;
182 const char**
const mArgv;
183 uint8_t
const mArgvSize;
184 const char*
const mPrompt;
ace_common::FCString getName() const
Return the name of the command.
Signature for a command handler.
void findAndRunCommand(const char *cmd, int argc, const char *const *argv) const
Find and run the given command.
void runCommand(char *line) const
Tokenize the given line and run the command handler.
static void printHelp(Print &printer, const CommandHandler *command)
Print help string for the given command.
void helpCommandHandler(Print &printer, int argc, const char *const *argv) const
Handle the 'help' command.
virtual ~CommandDispatcher()
Destructor.
void helpAll(Print &printer) const
Print help on all commands.
const CommandHandler * findCommand(const char *cmd) const
Find the CommandHandler of the given command name.
static uint8_t tokenize(char *line, const char **argv, uint8_t argvSize)
Tokenize the line, separating tokens delimited by whitespace (space, formfeed, carriage return,...
A coroutine that reads lines from the Serial port, tokenizes the line on whitespace boundaries,...
bool helpSpecific(Print &printer, const char *cmd) const
Print helpString of specific cmd.
CommandDispatcher(ace_routine::Channel< InputLine > &channel, Print &printer, const CommandHandler *const *commands, uint8_t numCommands, const char **argv, uint8_t argvSize, const char *prompt)
Constructor.
void printLineError(const char *line, uint8_t statusCode) const
Print the input line that caused an error along with its status code.