AceUtils  0.3
Useful Arduino utilties which are too small as separate libraries, but complex enough to have external dependencies to other libraries.
CommandHandler.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 CLI_COMMAND_HANDLER_H
26 #define CLI_COMMAND_HANDLER_H
27 
28 #include <AceCommon.h> // FCString
29 
30 class Print;
31 
39 #define SHIFT_ARGC_ARGV(argc, argv) do { argc--; argv++; } while (false)
40 
41 namespace cli {
42 
47  public:
63  virtual void run(Print& printer, int argc, const char* const* argv)
64  const = 0;
65 
67  ace_common::FCString getName() const { return mName; }
68 
70  ace_common::FCString getHelpString() const { return mHelpString; }
71 
72  protected:
74  static bool isArgEqual(const char* arg, const char* token) {
75  return strcmp(arg, token) == 0;
76  }
77 
79  static bool isArgEqual(const char* arg, const __FlashStringHelper* token) {
80  return strcmp_P(arg, (const char*) token) == 0;
81  }
82 
84  CommandHandler(const char* name, const char* helpString):
85  mName(name),
86  mHelpString(helpString) {}
87 
89  CommandHandler(const __FlashStringHelper* name,
90  const __FlashStringHelper* helpString):
91  mName(name),
92  mHelpString(helpString) {}
93 
94  private:
95  ace_common::FCString const mName;
96  ace_common::FCString const mHelpString;
97 };
98 
99 }
100 
101 #endif
cli::CommandHandler::CommandHandler
CommandHandler(const __FlashStringHelper *name, const __FlashStringHelper *helpString)
Constructor.
Definition: CommandHandler.h:89
cli::CommandHandler::CommandHandler
CommandHandler(const char *name, const char *helpString)
Constructor.
Definition: CommandHandler.h:84
cli::CommandHandler
Signature for a command handler.
Definition: CommandHandler.h:46
cli::CommandHandler::isArgEqual
static bool isArgEqual(const char *arg, const __FlashStringHelper *token)
Test for equality when token is in PROGMEM.
Definition: CommandHandler.h:79
cli::CommandHandler::getHelpString
ace_common::FCString getHelpString() const
Return the help string of the command.
Definition: CommandHandler.h:70
cli::CommandHandler::run
virtual void run(Print &printer, int argc, const char *const *argv) const =0
Run the command.
cli::CommandHandler::isArgEqual
static bool isArgEqual(const char *arg, const char *token)
Test for equality against token.
Definition: CommandHandler.h:74
cli::CommandHandler::getName
ace_common::FCString getName() const
Return the name of the command.
Definition: CommandHandler.h:67