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