Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 22 | |
| 23 | #include "command.h" |
| 24 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 25 | namespace simpleperf { |
| 26 | namespace { |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 27 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 28 | class HelpCommand : public Command { |
| 29 | public: |
| 30 | HelpCommand() |
| 31 | : Command("help", "print help information for simpleperf", |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 32 | // clang-format off |
| 33 | "Usage: simpleperf help [subcommand]\n" |
| 34 | " Without subcommand, print short help string for every subcommand.\n" |
| 35 | " With subcommand, print long help string for the subcommand.\n\n" |
| 36 | // clang-format on |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 37 | ) {} |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 38 | |
| 39 | bool Run(const std::vector<std::string>& args) override; |
| 40 | |
| 41 | private: |
| 42 | void PrintShortHelp(); |
| 43 | void PrintLongHelpForOneCommand(const Command& cmd); |
| 44 | }; |
| 45 | |
| 46 | bool HelpCommand::Run(const std::vector<std::string>& args) { |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 47 | if (args.empty()) { |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 48 | PrintShortHelp(); |
| 49 | } else { |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 50 | std::unique_ptr<Command> cmd = CreateCommandInstance(args[0]); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 51 | if (cmd == nullptr) { |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 52 | LOG(ERROR) << "malformed command line: can't find help string for " |
| 53 | "unknown command " |
| 54 | << args[0]; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 55 | LOG(ERROR) << "try using \"--help\""; |
| 56 | return false; |
| 57 | } else { |
| 58 | PrintLongHelpForOneCommand(*cmd); |
| 59 | } |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | void HelpCommand::PrintShortHelp() { |
Yabin Cui | 6d2db33 | 2015-06-30 18:03:34 -0700 | [diff] [blame] | 65 | printf( |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 66 | // clang-format off |
| 67 | "Usage: simpleperf [common options] subcommand [args_for_subcommand]\n" |
| 68 | "common options:\n" |
| 69 | " -h/--help Print this help information.\n" |
| 70 | " --log <severity> Set the minimum severity of logging. Possible severities\n" |
| 71 | " include verbose, debug, warning, info, error, fatal.\n" |
| 72 | " Default is info.\n" |
Yabin Cui | d9121ce | 2019-02-07 11:06:16 -0800 | [diff] [blame] | 73 | #if defined(__ANDROID__) |
| 74 | " --log-to-android-buffer Write log to android log buffer instead of stderr.\n" |
| 75 | #endif |
Yabin Cui | 6ef55f7 | 2016-07-26 18:42:38 -0700 | [diff] [blame] | 76 | " --version Print version of simpleperf.\n" |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 77 | "subcommands:\n" |
| 78 | // clang-format on |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 79 | ); |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 80 | for (auto& cmd_name : GetAllCommandNames()) { |
| 81 | std::unique_ptr<Command> cmd = CreateCommandInstance(cmd_name); |
Yabin Cui | 6d2db33 | 2015-06-30 18:03:34 -0700 | [diff] [blame] | 82 | printf(" %-20s%s\n", cmd_name.c_str(), cmd->ShortHelpString().c_str()); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | void HelpCommand::PrintLongHelpForOneCommand(const Command& command) { |
| 87 | printf("%s\n", command.LongHelpString().c_str()); |
| 88 | } |
| 89 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 90 | } // namespace |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 91 | |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 92 | void RegisterHelpCommand() { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 93 | RegisterCommand("help", [] { return std::unique_ptr<Command>(new HelpCommand); }); |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 94 | } |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 95 | |
| 96 | } // namespace simpleperf |