blob: 7054e65a645bf744e4462d70f94edb080114b6d4 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
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
Elliott Hughes66dd09e2015-12-04 14:00:57 -080021#include <android-base/logging.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070022
23#include "command.h"
24
25class HelpCommand : public Command {
26 public:
27 HelpCommand()
28 : Command("help", "print help information for simpleperf",
Yabin Cui767dd172016-06-02 21:02:43 -070029 // clang-format off
30"Usage: simpleperf help [subcommand]\n"
31" Without subcommand, print short help string for every subcommand.\n"
32" With subcommand, print long help string for the subcommand.\n\n"
33 // clang-format on
34 ) {}
Yabin Cui67d3abd2015-04-16 15:26:31 -070035
36 bool Run(const std::vector<std::string>& args) override;
37
38 private:
39 void PrintShortHelp();
40 void PrintLongHelpForOneCommand(const Command& cmd);
41};
42
43bool HelpCommand::Run(const std::vector<std::string>& args) {
Yabin Cuif79f07e2015-06-01 11:21:37 -070044 if (args.empty()) {
Yabin Cui67d3abd2015-04-16 15:26:31 -070045 PrintShortHelp();
46 } else {
Yabin Cuif79f07e2015-06-01 11:21:37 -070047 std::unique_ptr<Command> cmd = CreateCommandInstance(args[0]);
Yabin Cui67d3abd2015-04-16 15:26:31 -070048 if (cmd == nullptr) {
Yabin Cui767dd172016-06-02 21:02:43 -070049 LOG(ERROR) << "malformed command line: can't find help string for "
50 "unknown command "
51 << args[0];
Yabin Cui67d3abd2015-04-16 15:26:31 -070052 LOG(ERROR) << "try using \"--help\"";
53 return false;
54 } else {
55 PrintLongHelpForOneCommand(*cmd);
56 }
57 }
58 return true;
59}
60
61void HelpCommand::PrintShortHelp() {
Yabin Cui6d2db332015-06-30 18:03:34 -070062 printf(
Yabin Cui767dd172016-06-02 21:02:43 -070063 // clang-format off
64"Usage: simpleperf [common options] subcommand [args_for_subcommand]\n"
65"common options:\n"
66" -h/--help Print this help information.\n"
67" --log <severity> Set the minimum severity of logging. Possible severities\n"
68" include verbose, debug, warning, info, error, fatal.\n"
69" Default is info.\n"
Yabin Cui6ef55f72016-07-26 18:42:38 -070070" --version Print version of simpleperf.\n"
Yabin Cui767dd172016-06-02 21:02:43 -070071 "subcommands:\n"
72 // clang-format on
73 );
Yabin Cuif79f07e2015-06-01 11:21:37 -070074 for (auto& cmd_name : GetAllCommandNames()) {
75 std::unique_ptr<Command> cmd = CreateCommandInstance(cmd_name);
Yabin Cui6d2db332015-06-30 18:03:34 -070076 printf(" %-20s%s\n", cmd_name.c_str(), cmd->ShortHelpString().c_str());
Yabin Cui67d3abd2015-04-16 15:26:31 -070077 }
78}
79
80void HelpCommand::PrintLongHelpForOneCommand(const Command& command) {
81 printf("%s\n", command.LongHelpString().c_str());
82}
83
Yabin Cuiff7465c2016-02-25 11:02:30 -080084void RegisterHelpCommand() {
Yabin Cui767dd172016-06-02 21:02:43 -070085 RegisterCommand("help",
86 [] { return std::unique_ptr<Command>(new HelpCommand); });
Yabin Cuif79f07e2015-06-01 11:21:37 -070087}