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.
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 ACE_UTILS_CLI_COMMAND_HANDLER_H
26 #define ACE_UTILS_CLI_COMMAND_HANDLER_H
27 
28 #include <AceCommon.h> // FCString
29 
30 class Print;
31 
32 namespace ace_utils {
33 namespace cli {
34 
39  public:
55  virtual void run(Print& printer, int argc, const char* const* argv)
56  const = 0;
57 
59  ace_common::FCString getName() const { return mName; }
60 
62  ace_common::FCString getHelpString() const { return mHelpString; }
63 
64  protected:
72  static void shiftArgcArgv(int& argc, const char* const*& argv) {
73  argc--;
74  argv++;
75  }
76 
78  static void SHIFT_ARGC_ARGV(int& argc, const char* const*& argv) {
79  shiftArgcArgv(argc, argv);
80  }
81 
83  static bool isArgEqual(const char* arg, const char* token) {
84  return strcmp(arg, token) == 0;
85  }
86 
88  static bool isArgEqual(const char* arg, const __FlashStringHelper* token) {
89  return strcmp_P(arg, (const char*) token) == 0;
90  }
91 
93  CommandHandler(const char* name, const char* helpString):
94  mName(name),
95  mHelpString(helpString) {}
96 
98  CommandHandler(const __FlashStringHelper* name,
99  const __FlashStringHelper* helpString):
100  mName(name),
101  mHelpString(helpString) {}
102 
103  private:
104  ace_common::FCString const mName;
105  ace_common::FCString const mHelpString;
106 };
107 
108 } // cli
109 } // ace_utils
110 
111 #endif
Signature for a command handler.
ace_common::FCString getHelpString() const
Return the help string of the command.
ace_common::FCString getName() const
Return the name of the command.
CommandHandler(const char *name, const char *helpString)
Constructor.
static void shiftArgcArgv(int &argc, const char *const *&argv)
Increment argv and decrement argc so that it appears as if the command line arguments are shifted to ...
static void SHIFT_ARGC_ARGV(int &argc, const char *const *&argv)
Backwards compatibility with previous macro implementation .
virtual void run(Print &printer, int argc, const char *const *argv) const =0
Run the command.
CommandHandler(const __FlashStringHelper *name, const __FlashStringHelper *helpString)
Constructor.
static bool isArgEqual(const char *arg, const char *token)
Test for equality against token.
static bool isArgEqual(const char *arg, const __FlashStringHelper *token)
Test for equality when token is in PROGMEM.