1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <string>
19 #include <vector>
20
21 #include <android-base/logging.h>
22
23 #include "command.h"
24
25 class HelpCommand : public Command {
26 public:
HelpCommand()27 HelpCommand()
28 : Command("help", "print help information for simpleperf",
29 "Usage: simpleperf help [subcommand]\n"
30 " Without subcommand, print short help string for every subcommand.\n"
31 " With subcommand, print long help string for the subcommand.\n\n") {
32 }
33
34 bool Run(const std::vector<std::string>& args) override;
35
36 private:
37 void PrintShortHelp();
38 void PrintLongHelpForOneCommand(const Command& cmd);
39 };
40
Run(const std::vector<std::string> & args)41 bool HelpCommand::Run(const std::vector<std::string>& args) {
42 if (args.empty()) {
43 PrintShortHelp();
44 } else {
45 std::unique_ptr<Command> cmd = CreateCommandInstance(args[0]);
46 if (cmd == nullptr) {
47 LOG(ERROR) << "malformed command line: can't find help string for unknown command " << args[0];
48 LOG(ERROR) << "try using \"--help\"";
49 return false;
50 } else {
51 PrintLongHelpForOneCommand(*cmd);
52 }
53 }
54 return true;
55 }
56
PrintShortHelp()57 void HelpCommand::PrintShortHelp() {
58 printf(
59 "Usage: simpleperf [common options] subcommand [args_for_subcommand]\n"
60 "common options:\n"
61 " -h/--help Print this help information.\n"
62 " --log <severity> Set the minimum severity of logging. Possible severities\n"
63 " include verbose, debug, warning, error, fatal. Default is\n"
64 " warning.\n"
65 "subcommands:\n");
66 for (auto& cmd_name : GetAllCommandNames()) {
67 std::unique_ptr<Command> cmd = CreateCommandInstance(cmd_name);
68 printf(" %-20s%s\n", cmd_name.c_str(), cmd->ShortHelpString().c_str());
69 }
70 }
71
PrintLongHelpForOneCommand(const Command & command)72 void HelpCommand::PrintLongHelpForOneCommand(const Command& command) {
73 printf("%s\n", command.LongHelpString().c_str());
74 }
75
RegisterHelpCommand()76 void RegisterHelpCommand() {
77 RegisterCommand("help", [] { return std::unique_ptr<Command>(new HelpCommand); });
78 }
79