1 /*
2 * Copyright (c) 2021 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 "command.h"
17 #include "debug_logger.h"
18 #include "option.h"
19 #include "subcommand.h"
20 #include "utilities.h"
21 namespace OHOS {
22 namespace Developtools {
23 namespace HiPerf {
24 std::string Command::fullArgument;
DispatchCommands(std::vector<std::string> arguments)25 bool Command::DispatchCommands(std::vector<std::string> arguments)
26 {
27 for (std::string arg : arguments) {
28 fullArgument.append(" ");
29 fullArgument.append(arg);
30 }
31 HLOGD("args:%s", VectorToString(arguments).c_str());
32 while (!arguments.empty()) {
33 // we need found main command args first
34 auto commandOption = Option::FindMainOption(arguments.front());
35 if (commandOption != nullptr) {
36 // this is a arg which we support
37
38 // remove the arg name
39 arguments.erase(arguments.begin());
40
41 if (!commandOption->callBackFunction(arguments)) {
42 printf("unknown options: %s\nUse the help command to view help.\n", arguments.front().c_str());
43 return false;
44 }
45 // goto next args
46 continue;
47 } else {
48 // if it is an sub command
49 auto subCommand = SubCommand::FindSubCommand(arguments.front());
50 if (subCommand != nullptr) {
51 // this is an sub command which we support
52
53 // remove the subcmd name
54 arguments.erase(arguments.begin());
55
56 // if we found the sub command , after it processed , we will exit
57 HLOGD("OnSubCommandOptions -> %s", subCommand->Name().c_str());
58 if (subCommand->OnSubCommandOptions(arguments)) {
59 // if some help cmd ?
60 if (subCommand->OnPreSubCommand()) {
61 return true;
62 }
63
64 HLOGD("OnSubCommand -> %s", subCommand->Name().c_str());
65 if (!subCommand->OnSubCommand(arguments)) {
66 printf("subcommand '%s' failed\n", subCommand->Name().c_str());
67 return false;
68 } else {
69 HLOGD("OnSubCommand successed");
70 return true;
71 }
72 } else {
73 HLOGD("OnSubCommandOptions interrupt the process.");
74 return false;
75 }
76 } else {
77 // we don't support this command
78 printf("unknown args: %s\n", arguments.front().c_str());
79 return false;
80 }
81 }
82 }
83 return false;
84 }
85
DispatchCommand(std::string argument)86 bool Command::DispatchCommand(std::string argument)
87 {
88 return Command::DispatchCommands(StringSplit(argument, " "));
89 }
90 } // namespace HiPerf
91 } // namespace Developtools
92 } // namespace OHOS
93