1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BASE_NOTIFICATION_CES_STANDARD_CESFWK_TOOLS_CEM_INCLUDE_SHELL_COMMAND_H 17 #define BASE_NOTIFICATION_CES_STANDARD_CESFWK_TOOLS_CEM_INCLUDE_SHELL_COMMAND_H 18 19 #include <functional> 20 #include <map> 21 #include <string> 22 #include <vector> 23 24 #include "errors.h" 25 26 namespace OHOS { 27 namespace EventFwk { 28 namespace { 29 constexpr char HELP_MSG_NO_OPTION[] = "error: you must specify an option at least.\n"; 30 constexpr int MIN_ARGUMENT_NUMBER = 2; 31 } // namespace 32 33 class ShellCommand { 34 public: 35 /** 36 * Constructor. 37 * 38 * @param argc Indicates the argument count. 39 * @param argv Indicates the argument values. 40 * @param name Indicates the tool name. 41 */ 42 ShellCommand(int argc, char *argv[], std::string name); 43 44 virtual ~ShellCommand(); 45 46 /** 47 * Processes the command. 48 * 49 * @return Returns result code. 50 */ 51 ErrCode OnCommand(); 52 53 /** 54 * Executes the command. 55 * 56 * @return Returns result. 57 */ 58 std::string ExecCommand(); 59 60 /** 61 * Gets the error message of the command. 62 * 63 * @return Returns the error message of the command. 64 */ 65 std::string GetCommandErrorMsg() const; 66 67 /** 68 * Creates the command map. 69 * 70 * @return Returns result code. 71 */ 72 virtual ErrCode CreateCommandMap() = 0; 73 74 /** 75 * Inits. 76 * 77 * @return Returns result code. 78 */ 79 virtual ErrCode Init() = 0; 80 81 protected: 82 int argc_; 83 char **argv_; 84 std::string cmd_; 85 std::vector<std::string> argList_; 86 std::string name_; 87 std::map<std::string, std::function<int()>> commandMap_; 88 std::string resultReceiver_; 89 }; 90 } // namespace EventFwk 91 } // namespace OHOS 92 #endif // BASE_NOTIFICATION_CES_STANDARD_CESFWK_TOOLS_CEM_INCLUDE_SHELL_COMMAND_H