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