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_DISTRIBUTED_NOTIFICATION_SERVICE_TOOLS_DUMP_INCLUDE_SHELL_COMMAND_H
17#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_TOOLS_DUMP_INCLUDE_SHELL_COMMAND_H
18
19#include <functional>
20#include <map>
21#include <string>
22#include <vector>
23
24#include "errors.h"
25
26namespace OHOS {
27namespace Notification {
28class ShellCommand {
29public:
30    /**
31     * @brief Constructor.
32     *
33     * @param argc Indicates the count of arguments.
34     * @param argv Indicates the arguments.
35     * @param name Indicates the tool name.
36     */
37    ShellCommand(int argc, char *argv[], std::string name);
38
39    /**
40     * @brief Destructor.
41     */
42    virtual ~ShellCommand();
43
44    /**
45     * @brief Process the command.
46     *
47     * @return Indicates the command result code.
48     */
49    ErrCode OnCommand();
50
51    /**
52     * @brief Execute the command.
53     *
54     * @return Indicates the output text.
55     */
56    std::string ExecCommand();
57
58    /**
59     * @brief Get the error message of the command.
60     *
61     * @return Indicates the error message.
62     */
63    std::string GetCommandErrorMsg() const;
64
65    /**
66     * @brief Get the error message of the unknown option.
67     *
68     * @param unknownOption Indicates the unknown option.
69     * @return Indicates the error message.
70     */
71    std::string GetUnknownOptionMsg(std::string &unknownOption) const;
72
73    /**
74     * @brief Get the message from the code.
75     *
76     * @param code Indicates the code.
77     * @return Indicates the message.
78     */
79    std::string GetMessageFromCode(int32_t code) const;
80
81    /**
82     * @brief Create the command map.
83     *
84     * @return Indicates the result code.
85     */
86    virtual ErrCode CreateCommandMap() = 0;
87
88    /**
89     * @brief The initialize function.
90     *
91     * @return Indicates the result code.
92     */
93    virtual ErrCode Init() = 0;
94
95protected:
96    static constexpr int32_t MIN_ARGUMENT_NUMBER = 2;
97    static constexpr int32_t MAX_ARGUMENT_NUMBER = 256;
98
99    int argc_;
100    char **argv_;
101
102    std::string cmd_;
103    std::vector<std::string> argList_;
104    std::string name_;
105    std::map<std::string, std::function<int()>> commandMap_;
106    std::map<int32_t, std::string> messageMap_;
107    std::string resultReceiver_;
108};
109}  // namespace Notification
110}  // namespace OHOS
111
112#endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_TOOLS_DUMP_INCLUDE_SHELL_COMMAND_H