From d4586180a674df11cbfbea1cf2ce527bf1ed4117 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sun, 5 Aug 2012 00:57:57 +0200 Subject: [PATCH] Debugger CLI: Improve "help" command Given a command name it prints the command's usage. --- .../cli/CommandLineUserInterface.cpp | 73 ++++++++++++++----- .../cli/CommandLineUserInterface.h | 3 +- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/apps/debugger/user_interface/cli/CommandLineUserInterface.cpp b/src/apps/debugger/user_interface/cli/CommandLineUserInterface.cpp index c00111291f..0d1c92b24b 100644 --- a/src/apps/debugger/user_interface/cli/CommandLineUserInterface.cpp +++ b/src/apps/debugger/user_interface/cli/CommandLineUserInterface.cpp @@ -56,16 +56,23 @@ private: struct CommandLineUserInterface::HelpCommand : CliCommand { HelpCommand(CommandLineUserInterface* userInterface) : - CliCommand("print a list of all commands", - "%s\n" - "Prints a list of all commands."), + CliCommand("print help for a command or a list of all commands", + "%s [ ]\n" + "Prints help for command , if given, or a list of all " + "commands\n" + "otherwise."), fUserInterface(userInterface) { } virtual void Execute(int argc, const char* const* argv, CliContext& context) { - fUserInterface->_PrintHelp(); + if (argc > 2) { + PrintUsage(argv[0]); + return; + } + + fUserInterface->_PrintHelp(argc == 2 ? argv[1] : NULL); } private: @@ -282,33 +289,63 @@ CommandLineUserInterface::_RegisterCommand(const BString& name, void CommandLineUserInterface::_ExecuteCommand(int argc, const char* const* argv) { - const char* commandName = argv[0]; + CommandEntry* commandEntry = _FindCommand(argv[0]); + if (commandEntry != NULL) + commandEntry->Command()->Execute(argc, argv, fContext); +} + + +CommandLineUserInterface::CommandEntry* +CommandLineUserInterface::_FindCommand(const char* commandName) +{ size_t commandNameLength = strlen(commandName); - CommandEntry* firstEntry = NULL; + // try to find an exact match first + CommandEntry* commandEntry = NULL; for (int32 i = 0; CommandEntry* entry = fCommands.ItemAt(i); i++) { - if (entry->Name().Compare(commandName, commandNameLength) == 0) { - if (firstEntry != NULL) { - printf("Ambiguous command \"%s\".\n", commandName); - return; - } - - firstEntry = entry; + if (entry->Name() == commandName) { + commandEntry = entry; + break; } } - if (firstEntry == NULL) { - printf("Unknown command \"%s\".\n", commandName); - return; + // If nothing found yet, try partial matches, but only, if they are + // unambiguous. + if (commandEntry == NULL) { + for (int32 i = 0; CommandEntry* entry = fCommands.ItemAt(i); i++) { + if (entry->Name().Compare(commandName, commandNameLength) == 0) { + if (commandEntry != NULL) { + printf("Error: Ambiguous command \"%s\".\n", commandName); + return NULL; + } + + commandEntry = entry; + } + } } - firstEntry->Command()->Execute(argc, argv, fContext); + if (commandEntry == NULL) { + printf("Error: Unknown command \"%s\".\n", commandName); + return NULL; + } + + return commandEntry; } void -CommandLineUserInterface::_PrintHelp() +CommandLineUserInterface::_PrintHelp(const char* commandName) { + // If a command name is given, print the usage for that one. + if (commandName != NULL) { + CommandEntry* commandEntry = _FindCommand(commandName); + if (commandEntry != NULL) + commandEntry->Command()->PrintUsage(commandEntry->Name().String()); + return; + } + + // No command name given -- print a list of all commands. + // determine longest command name int32 longestCommandName = 0; for (int32 i = 0; CommandEntry* entry = fCommands.ItemAt(i); i++) { diff --git a/src/apps/debugger/user_interface/cli/CommandLineUserInterface.h b/src/apps/debugger/user_interface/cli/CommandLineUserInterface.h index 736429b0b4..6bede606c3 100644 --- a/src/apps/debugger/user_interface/cli/CommandLineUserInterface.h +++ b/src/apps/debugger/user_interface/cli/CommandLineUserInterface.h @@ -64,7 +64,8 @@ private: CliCommand* command); void _ExecuteCommand(int argc, const char* const* argv); - void _PrintHelp(); + CommandEntry* _FindCommand(const char* commandName); + void _PrintHelp(const char* commandName); private: CliContext fContext;