1 /*
2  * Copyright (c) 2021 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 OHOS_ABILITY_RUNTIME_SHELL_COMMAND_H
17 #define OHOS_ABILITY_RUNTIME_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 AAFwk {
28 namespace {
29 const std::string HELP_MSG_NO_OPTION = "error: you must specify an option at least.";
30 
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     static constexpr int MAX_ARGUMENT_NUMBER = 4096;
52 
53     int argc_ = 0;
54     char** argv_ = nullptr;
55 
56     std::string cmd_;
57     std::vector<std::string> argList_;
58 
59     std::string name_;
60     std::map<std::string, std::function<int()>> commandMap_;
61     std::map<int32_t, std::string> messageMap_;
62 
63     std::string resultReceiver_ = "";
64 };
65 }  // namespace AAFwk
66 }  // namespace OHOS
67 
68 #endif  // OHOS_ABILITY_RUNTIME_SHELL_COMMAND_H
69