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