1 /*
2 * Copyright (c) 2021-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 "subcommand.h"
17 #include "debug_logger.h"
18 #include "option.h"
19
20 using namespace std;
21 namespace OHOS {
22 namespace Developtools {
23 namespace HiPerf {
24 static std::map<std::string, std::unique_ptr<SubCommand>> g_SubCommandsMap;
25
26 // parse option first
OnSubCommandOptions(std::vector<std::string> args)27 bool SubCommand::OnSubCommandOptions(std::vector<std::string> args)
28 {
29 // parse common first
30 if (!Option::GetOptionValue(args, "--dumpoptions", dumpOptions_)) {
31 return false;
32 }
33 if (!Option::GetOptionValue(args, "--help", showHelp_)
34 || !Option::GetOptionValue(args, "-h", showHelp_)) {
35 return false;
36 }
37
38 if (showHelp_) {
39 if (!args.empty()) {
40 printf("unknown option '%s'\n", args.front().c_str());
41 return false;
42 }
43 if (OnPreSubCommand()) {
44 return false;
45 }
46 }
47
48 if (ParseOption(args)) {
49 if (dumpOptions_) {
50 DumpOptions();
51 }
52 HLOGD(" args left over: (%zu): %s", args.size(), VectorToString(args).c_str());
53 if (!args.empty()) {
54 printf("unknown option '%s'\n", args.front().c_str());
55 return false;
56 }
57 } else {
58 HLOGD("incorrect option(s)\n");
59 return false;
60 }
61 return true;
62 }
63
RegisterSubCommand(std::string cmdName,std::unique_ptr<SubCommand> subCommand)64 bool SubCommand::RegisterSubCommand(std::string cmdName, std::unique_ptr<SubCommand> subCommand)
65 {
66 HLOGV("%s", cmdName.c_str());
67 if (cmdName.empty()) {
68 HLOGE("unable to register empty subcommand!");
69 return false;
70 }
71 if (cmdName.front() == '-') {
72 HLOGE("unable use '-' at the begin of subcommand '%s'", cmdName.c_str());
73 return false;
74 }
75
76 if (g_SubCommandsMap.count(cmdName) == 0) {
77 g_SubCommandsMap.insert(std::make_pair(cmdName, std::move(subCommand)));
78 return true;
79 } else {
80 HLOGE("subcommand '%s' already registered!", cmdName.c_str());
81 return false;
82 }
83 }
84
ClearSubCommands()85 void SubCommand::ClearSubCommands()
86 {
87 g_SubCommandsMap.clear();
88 }
89
GetSubCommands()90 const std::map<std::string, std::unique_ptr<SubCommand>> &SubCommand::GetSubCommands()
91 {
92 HLOGV("enter");
93 return g_SubCommandsMap;
94 }
95
FindSubCommand(std::string cmdName)96 SubCommand *SubCommand::FindSubCommand(std::string cmdName)
97 {
98 HLOGV("%s", cmdName.c_str());
99 auto found = g_SubCommandsMap.find(cmdName);
100 if (found != g_SubCommandsMap.end()) {
101 // remove the subcmd itself
102 return found->second.get();
103 } else {
104 return nullptr;
105 }
106 }
107 } // namespace HiPerf
108 } // namespace Developtools
109 } // namespace OHOS
110