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.
Public Member Functions | Static Public Member Functions | List of all members
ace_utils::cli::CommandDispatcher Class Reference

A class that tokenizes a line containing tokens separated on whitespace boundaries, and calls the appropriate command handler to handle the command. More...

#include <CommandDispatcher.h>

Public Member Functions

 CommandDispatcher (const CommandHandler *const *commands, uint8_t numCommands, const char **argv, uint8_t argvSize)
 Constructor. More...
 
void runCommand (Print &printer, char *line) const
 Tokenize the given 'line', run the matching command handler, and send output to the 'printer'.
 
const CommandHandlerfindCommand (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...
 

Detailed Description

A class that tokenizes a line containing tokens separated 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 50 of file CommandDispatcher.h.

Constructor & Destructor Documentation

◆ CommandDispatcher()

ace_utils::cli::CommandDispatcher::CommandDispatcher ( const CommandHandler *const *  commands,
uint8_t  numCommands,
const char **  argv,
uint8_t  argvSize 
)
inline

Constructor.

Parameters
commandsArray of CommandHandler pointers.
numCommandsnumber of commands.
argvArray of (const char*) that will be used to hold the word tokens of a command line string.
argvSizeThe size of the argv array. Tokens which are beyond this limit will be silently dropped.

Definition at line 62 of file CommandDispatcher.h.

Member Function Documentation

◆ findCommand()

const CommandHandler* ace_utils::cli::CommandDispatcher::findCommand ( const char *  cmd) const
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 108 of file CommandDispatcher.h.

◆ tokenize()

static uint8_t ace_utils::cli::CommandDispatcher::tokenize ( char *  line,
const char **  argv,
uint8_t  argvSize 
)
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 88 of file CommandDispatcher.h.


The documentation for this class was generated from the following files: