Debugger CLI: Improve "help" command
Given a command name it prints the command's usage.
This commit is contained in:
@@ -56,16 +56,23 @@ private:
|
|||||||
struct CommandLineUserInterface::HelpCommand : CliCommand {
|
struct CommandLineUserInterface::HelpCommand : CliCommand {
|
||||||
HelpCommand(CommandLineUserInterface* userInterface)
|
HelpCommand(CommandLineUserInterface* userInterface)
|
||||||
:
|
:
|
||||||
CliCommand("print a list of all commands",
|
CliCommand("print help for a command or a list of all commands",
|
||||||
"%s\n"
|
"%s [ <command> ]\n"
|
||||||
"Prints a list of all commands."),
|
"Prints help for command <command>, if given, or a list of all "
|
||||||
|
"commands\n"
|
||||||
|
"otherwise."),
|
||||||
fUserInterface(userInterface)
|
fUserInterface(userInterface)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void Execute(int argc, const char* const* argv, CliContext& context)
|
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:
|
private:
|
||||||
@@ -282,33 +289,63 @@ CommandLineUserInterface::_RegisterCommand(const BString& name,
|
|||||||
void
|
void
|
||||||
CommandLineUserInterface::_ExecuteCommand(int argc, const char* const* argv)
|
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);
|
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() == commandName) {
|
||||||
|
commandEntry = entry;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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++) {
|
for (int32 i = 0; CommandEntry* entry = fCommands.ItemAt(i); i++) {
|
||||||
if (entry->Name().Compare(commandName, commandNameLength) == 0) {
|
if (entry->Name().Compare(commandName, commandNameLength) == 0) {
|
||||||
if (firstEntry != NULL) {
|
if (commandEntry != NULL) {
|
||||||
printf("Ambiguous command \"%s\".\n", commandName);
|
printf("Error: Ambiguous command \"%s\".\n", commandName);
|
||||||
return;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
firstEntry = entry;
|
commandEntry = entry;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (firstEntry == NULL) {
|
if (commandEntry == NULL) {
|
||||||
printf("Unknown command \"%s\".\n", commandName);
|
printf("Error: Unknown command \"%s\".\n", commandName);
|
||||||
return;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
firstEntry->Command()->Execute(argc, argv, fContext);
|
return commandEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
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
|
// determine longest command name
|
||||||
int32 longestCommandName = 0;
|
int32 longestCommandName = 0;
|
||||||
for (int32 i = 0; CommandEntry* entry = fCommands.ItemAt(i); i++) {
|
for (int32 i = 0; CommandEntry* entry = fCommands.ItemAt(i); i++) {
|
||||||
|
|||||||
@@ -64,7 +64,8 @@ private:
|
|||||||
CliCommand* command);
|
CliCommand* command);
|
||||||
void _ExecuteCommand(int argc,
|
void _ExecuteCommand(int argc,
|
||||||
const char* const* argv);
|
const char* const* argv);
|
||||||
void _PrintHelp();
|
CommandEntry* _FindCommand(const char* commandName);
|
||||||
|
void _PrintHelp(const char* commandName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CliContext fContext;
|
CliContext fContext;
|
||||||
|
|||||||
Reference in New Issue
Block a user