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