summaryrefslogtreecommitdiff
path: root/simpleperf/cmd_help.cpp
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2015-06-01 11:21:37 -0700
committerYabin Cui <yabinc@google.com>2015-06-04 15:26:32 -0700
commitf79f07e13c56f7ca3be1435cea7f8861daf7efaa (patch)
tree8c76bac6fe4d9b52b69e57393f5deecb18febd97 /simpleperf/cmd_help.cpp
parentd4637d6e7d17f48d9325fa133be82b06a408f523 (diff)
downloadextras-f79f07e13c56f7ca3be1435cea7f8861daf7efaa.tar.gz
Simpleperf: refactor command system.
Register a callback function to create a new command instance instead of registering a command instance. Then we can release resources in the command destructors, and don't need xxxCommandImpl classes any more. Bug: 19483574 Change-Id: Ibb54892ec0655fd43909347afd72bb08bc8a716c
Diffstat (limited to 'simpleperf/cmd_help.cpp')
-rw-r--r--simpleperf/cmd_help.cpp13
1 files changed, 8 insertions, 5 deletions
diff --git a/simpleperf/cmd_help.cpp b/simpleperf/cmd_help.cpp
index 0f3839b3..75df732a 100644
--- a/simpleperf/cmd_help.cpp
+++ b/simpleperf/cmd_help.cpp
@@ -39,10 +39,10 @@ class HelpCommand : public Command {
};
bool HelpCommand::Run(const std::vector<std::string>& args) {
- if (args.size() == 1) {
+ if (args.empty()) {
PrintShortHelp();
} else {
- Command* cmd = Command::FindCommandByName(args[1]);
+ std::unique_ptr<Command> cmd = CreateCommandInstance(args[0]);
if (cmd == nullptr) {
LOG(ERROR) << "malformed command line: can't find help string for unknown command " << args[0];
LOG(ERROR) << "try using \"--help\"";
@@ -56,8 +56,9 @@ bool HelpCommand::Run(const std::vector<std::string>& args) {
void HelpCommand::PrintShortHelp() {
printf("Usage: simpleperf [--help] subcommand [args_for_subcommand]\n\n");
- for (auto& command : Command::GetAllCommands()) {
- printf("%-20s%s\n", command->Name().c_str(), command->ShortHelpString().c_str());
+ for (auto& cmd_name : GetAllCommandNames()) {
+ std::unique_ptr<Command> cmd = CreateCommandInstance(cmd_name);
+ printf("%-20s%s\n", cmd_name.c_str(), cmd->ShortHelpString().c_str());
}
}
@@ -65,4 +66,6 @@ void HelpCommand::PrintLongHelpForOneCommand(const Command& command) {
printf("%s\n", command.LongHelpString().c_str());
}
-HelpCommand help_command;
+__attribute__((constructor)) static void RegisterHelpCommand() {
+ RegisterCommand("help", [] { return std::unique_ptr<Command>(new HelpCommand); });
+}