1 /*
2 * Copyright (c) 2022-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 #include "time_cmd_dispatcher.h"
17
18 namespace OHOS {
19 namespace MiscServices {
Dispatch(int fd,const std::vector<std::string> & args)20 bool TimeCmdDispatcher::Dispatch(int fd, const std::vector<std::string> &args)
21 {
22 if (args.empty() || args.at(0) == "-h") {
23 dprintf(fd, "\n%-15s: %-20s", "Option", "Description");
24 for (auto &[key, handler] : cmdHandler) {
25 dprintf(fd, "\n%-15s: %-20s", handler->GetFormat().c_str(), handler->ShowHelp().c_str());
26 }
27 return false;
28 }
29 std::string cmdTitle = getCmdTitle(fd, args);
30 auto handler = cmdHandler.find(cmdTitle);
31 if (handler != cmdHandler.end()) {
32 handler->second->DoAction(fd, args);
33 return true;
34 }
35 return false;
36 }
37
RegisterCommand(std::shared_ptr<TimeCmdParse> & cmd)38 void TimeCmdDispatcher::RegisterCommand(std::shared_ptr<TimeCmdParse> &cmd)
39 {
40 cmdHandler.insert(std::make_pair(cmd->GetOption(), cmd));
41 }
42
getCmdTitle(int fd,const std::vector<std::string> & args)43 std::string TimeCmdDispatcher::getCmdTitle(int fd, const std::vector<std::string> &args)
44 {
45 std::string formatTitle;
46 if (args.size() == 0) {
47 return formatTitle;
48 }
49 for (unsigned long i = 0; i < args.size() - 1; i++) {
50 formatTitle += args.at(i);
51 formatTitle += " ";
52 }
53 return formatTitle.substr(0, formatTitle.length() - 1);
54 dprintf(fd, "TimeCmdDispatcher::getCmdTitle formatTitle:%s.\n", formatTitle.c_str());
55 }
56
GetInstance()57 TimeCmdDispatcher &TimeCmdDispatcher::GetInstance()
58 {
59 static TimeCmdDispatcher instance;
60 return instance;
61 }
62 } // namespace MiscServices
63 } // namespace OHOS