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 printf("\t%-20s\t%s\n", commandOption.first.c_str(),
46 commandOption.second->help.c_str());
47 }
48
49 auto &commands = SubCommand::GetSubCommands();
50 HLOGD("%zu cmds found:", commands.size());
51 printf("command:\n");
52 for (const auto &command : commands) {
53 printf("\t%s:\t%s\n", command.second->Name().c_str(), command.second->Brief().c_str());
54 }
55 printf("\nSee 'hiperf help [command]' for more information on a specific command.\n\n");
56 } else {
57 auto command = SubCommand::FindSubCommand(args.front());
58 if (command != nullptr) {
59 args.clear();
60 printf("%s\n", command->Help().c_str());
61 } else {
62 printf("Unknown command: '%s'\n", args.front().c_str());
63 return false;
64 }
65 }
66
67 return true;
68 }
69 } // namespace HiPerf
70 } // namespace Developtools
71 } // namespace OHOS
72