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 "subcommand_help.h"
17 #include "debug_logger.h"
18 #include "utilities.h"
19
20 using namespace std;
21
22 namespace OHOS {
23 namespace Developtools {
24 namespace HiPerf {
OnSubCommand(std::vector<std::string> & args)25 bool SubCommandHelp::OnSubCommand(std::vector<std::string> &args)
26 {
27 HLOGV("enter");
28 OnHelp(args);
29 return true;
30 }
31
RegisterSubCommandHelp()32 void SubCommandHelp::RegisterSubCommandHelp()
33 {
34 HLOGV("enter");
35 SubCommand::RegisterSubCommand("help", std::make_unique<SubCommandHelp>());
36 }
37
OnHelp(std::vector<std::string> & args)38 bool SubCommandHelp::OnHelp(std::vector<std::string> &args)
39 {
40 if (args.empty()) {
41 const auto &mainOptions = Option::GetMainOptions();
42 HLOGD("%zu options found:", mainOptions.size());
43 printf("Usage: hiperf [options] command [args for command]\n");
44
45 printf("options:\n");
46 for (const auto &commandOption : mainOptions) {
47 printf("\t%-20s\t%s\n", commandOption.first.c_str(),
48 commandOption.second->help.c_str());
49 }
50
51 auto &commands = SubCommand::GetSubCommands();
52 HLOGD("%zu cmds found:", commands.size());
53 printf("command:\n");
54 for (const auto &command : commands) {
55 printf("\t%s:\t%s\n", command.second->Name().c_str(), command.second->Brief().c_str());
56 }
57 printf("\nSee 'hiperf help [command]' for more information on a specific command.\n\n");
58 } else {
59 auto command = SubCommand::FindSubCommand(args.front());
60 if (command != nullptr) {
61 args.clear();
62 printf("%s\n", command->Help().c_str());
63 } else {
64 printf("Unknown command: '%s'\n", args.front().c_str());
65 return false;
66 }
67 }
68
69 return true;
70 }
71 } // namespace HiPerf
72 } // namespace Developtools
73 } // namespace OHOS