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