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.
|
A coroutine that reads lines from the Serial port, tokenizes the line on whitespace boundaries, and calls the appropriate command handler to handle the command. More...
#include <CommandDispatcher.h>
Public Member Functions | |
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. More... | |
virtual | ~CommandDispatcher () |
Destructor. More... | |
int | runCoroutine () override |
const CommandHandler * | findCommand (const char *cmd) const |
Find the CommandHandler of the given command name. More... | |
Static Public Member Functions | |
static uint8_t | tokenize (char *line, const char **argv, uint8_t argvSize) |
Tokenize the line, separating tokens delimited by whitespace (space, formfeed, carriage return, newline, tab, and vertical tab) and fill argv with each token until argvSize is reached. More... | |
Protected Member Functions | |
CommandDispatcher (const CommandDispatcher &)=delete | |
CommandDispatcher & | operator= (const CommandDispatcher &)=delete |
void | printLineError (const char *line, uint8_t statusCode) const |
Print the input line that caused an error along with its status code. | |
void | helpCommandHandler (Print &printer, int argc, const char *const *argv) const |
Handle the 'help' command. | |
void | helpAll (Print &printer) const |
Print help on all commands. | |
bool | helpSpecific (Print &printer, const char *cmd) const |
Print helpString of specific cmd. More... | |
void | runCommand (char *line) const |
Tokenize the given line and run the command handler. | |
void | findAndRunCommand (const char *cmd, int argc, const char *const *argv) const |
Find and run the given command. | |
Static Protected Member Functions | |
static void | printHelp (Print &printer, const CommandHandler *command) |
Print help string for the given command. | |
Protected Attributes | |
ace_routine::Channel< InputLine > & | mChannel |
Print & | mPrinter |
const CommandHandler *const *const | mCommands |
const uint8_t | mNumCommands |
const char **const | mArgv |
const uint8_t | mArgvSize |
const char *const | mPrompt |
A coroutine that reads lines from the Serial port, tokenizes the line on whitespace boundaries, and calls the appropriate command handler to handle the command.
Command have the form "command arg1 arg2 ...", where the 'arg*' can be any string.
The calling code is expected to provide a mapping of the command string (e.g. "list") to its command handler (CommandHandler). The CommandHandler is called with the number of arguments (argc) and the array of tokens (argv), just like the arguments of the C-language main() function.
The template parameters can be either a 'char' for C-strings or '__FlashStringHelper' for F() flash strings.
Definition at line 52 of file CommandDispatcher.h.
|
inline |
Constructor.
channel | A channel from the StringLineReader to this coroutine. |
printer | The output object, normally the global Serial object. |
commands | Array of CommandHandler pointers. |
numCommands | number of commands. |
argv | Array of (const char*) that will be used to hold the word tokens of a command line string. |
argvSize | The size of the argv array. Tokens which are beyond this limit will be silently dropped. |
prompt | If not null, print a prompt and echo the command entered by the user. If null, don't print prompt and don't echo. |
Definition at line 68 of file CommandDispatcher.h.
|
inlinevirtual |
|
inline |
Find the CommandHandler of the given command name.
VisibleForTesting.
NOTE: this is currently a linear O(N) scan which is good enough for small number of commands. If we sorted the handlers, we could do a binary search for O(log(N)) and handle larger number of commands.
Definition at line 135 of file CommandDispatcher.h.
|
protected |
Print helpString of specific cmd.
Returns true if cmd was found.
Definition at line 85 of file CommandDispatcher.cpp.
|
inlinestatic |
Tokenize the line, separating tokens delimited by whitespace (space, formfeed, carriage return, newline, tab, and vertical tab) and fill argv with each token until argvSize is reached.
Return the number of tokens filled in.
VisibleForTesting.
Definition at line 95 of file CommandDispatcher.h.