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 FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_SHELL_COMMAND_H
17 #define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_SHELL_COMMAND_H
18 
19 #include <map>
20 #include <string>
21 #include <functional>
22 #include <vector>
23 
24 #include "errors.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const std::string HELP_MSG_NO_OPTION = "error: you must specify an option at least.";
30 const std::string STRING_CODE = "code:";
31 const int OFFSET_REQUIRED_ARGUMENT = 2;
32 }  // namespace
33 
34 class ShellCommand {
35 public:
36     ShellCommand(int argc, char *argv[], std::string name);
37     virtual ~ShellCommand();
38 
39     ErrCode OnCommand();
40     std::string ExecCommand();
41     std::string GetCommandErrorMsg() const;
42     std::string GetUnknownOptionMsg(std::string &unknownOption) const;
43     std::string GetMessageFromCode(const int32_t code) const;
44 
45     virtual ErrCode CreateCommandMap() = 0;
46     virtual ErrCode CreateMessageMap() = 0;
47     virtual ErrCode Init() = 0;
48 
49 protected:
50     static constexpr int MIN_ARGUMENT_NUMBER = 2;
51 
52     int argc_;
53     char **argv_;
54 
55     std::string cmd_;
56     std::vector<std::string> argList_;
57 
58     std::string name_;
59     std::map<std::string, std::function<int()>> commandMap_;
60     std::map<int32_t, std::string> messageMap_;
61 
62     std::string resultReceiver_ = "";
63 };
64 
65 }  // namespace AppExecFwk
66 }  // namespace OHOS
67 
68 #endif  // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_SHELL_COMMAND_H