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