1 /*
2  * Copyright (C) 2022-2023 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 #include "pasteboard_dump_helper.h"
17 
18 #include <cstdio>
19 
20 namespace OHOS {
21 namespace MiscServices {
Dump(int fd,const std::vector<std::string> & args) const22 bool PasteboardDumpHelper::Dump(int fd, const std::vector<std::string> &args) const
23 {
24     dprintf(fd, "\n---------------------------------\n");
25     if (args.empty() || args.at(0) == "-h") {
26         printf("\n%-15s: %-20s", "Option", "Description");
27         for (auto &[key, handler] : cmdHandler) {
28             dprintf(fd, "\n%-15s: %-20s\n", handler->GetFormat().c_str(), handler->ShowHelp().c_str());
29         }
30         return false;
31     }
32     auto handler = cmdHandler.find(args.at(0));
33     if (handler != cmdHandler.end()) {
34         std::string output;
35         handler->second->DoAction(args, output);
36         dprintf(fd, "%s\n", output.c_str());
37         return true;
38     }
39     return false;
40 }
RegisterCommand(std::shared_ptr<Command> & cmd)41 void PasteboardDumpHelper::RegisterCommand(std::shared_ptr<Command> &cmd)
42 {
43     cmdHandler.insert(std::make_pair(cmd->GetOption(), cmd));
44 }
45 
GetInstance()46 PasteboardDumpHelper &PasteboardDumpHelper::GetInstance()
47 {
48     static PasteboardDumpHelper instance;
49     return instance;
50 }
51 } // namespace MiscServices
52 } // namespace OHOS
53