2010-06-08 16:52:24 +00:00
//===-- CommandObjectHelp.cpp -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
2016-02-20 00:58:29 +00:00
# include "CommandObjectHelp.h"
2010-06-08 16:52:24 +00:00
# include "lldb/Interpreter/CommandObjectMultiword.h"
# include "lldb/Interpreter/CommandInterpreter.h"
2010-06-15 19:49:27 +00:00
# include "lldb/Interpreter/Options.h"
2010-06-08 16:52:24 +00:00
# include "lldb/Interpreter/CommandReturnObject.h"
using namespace lldb ;
using namespace lldb_private ;
//-------------------------------------------------------------------------
// CommandObjectHelp
//-------------------------------------------------------------------------
2016-02-29 23:22:53 +00:00
void
CommandObjectHelp : : GenerateAdditionalHelpAvenuesMessage ( Stream * s ,
const char * command ,
const char * prefix ,
const char * subcommand ,
bool include_apropos ,
bool include_type_lookup )
{
if ( s & & command & & * command )
{
s - > Printf ( " '%s' is not a known command. \n " , command ) ;
2016-04-21 00:55:20 +00:00
s - > Printf ( " Try '%shelp' to see a current list of commands. \n " , prefix ? prefix : " " ) ;
2016-02-29 23:22:53 +00:00
if ( include_apropos )
{
2016-04-21 00:55:20 +00:00
s - > Printf ( " Try '%sapropos %s' for a list of related commands. \n " ,
prefix ? prefix : " " , subcommand ? subcommand : command ) ;
2016-02-29 23:22:53 +00:00
}
if ( include_type_lookup )
{
2016-04-21 00:55:20 +00:00
s - > Printf ( " Try '%stype lookup %s' for information on types, methods, functions, modules, etc. " ,
prefix ? prefix : " " , subcommand ? subcommand : command ) ;
2016-02-29 23:22:53 +00:00
}
}
}
2016-07-14 22:03:10 +00:00
CommandObjectHelp : : CommandObjectHelp ( CommandInterpreter & interpreter )
: CommandObjectParsed ( interpreter , " help " ,
" Show a list of all debugger commands, or give details about a specific command. " ,
" help [<cmd-name>] " ) ,
m_options ( interpreter )
2010-06-08 16:52:24 +00:00
{
2010-10-04 22:28:36 +00:00
CommandArgumentEntry arg ;
CommandArgumentData command_arg ;
// Define the first (and only) variant of this arg.
command_arg . arg_type = eArgTypeCommandName ;
command_arg . arg_repetition = eArgRepeatStar ;
// There is only one variant this argument could be; put it into the argument entry.
arg . push_back ( command_arg ) ;
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg ) ;
2010-06-08 16:52:24 +00:00
}
2016-02-20 00:58:29 +00:00
CommandObjectHelp : : ~ CommandObjectHelp ( ) = default ;
2010-06-08 16:52:24 +00:00
2011-09-09 17:49:36 +00:00
OptionDefinition
CommandObjectHelp : : CommandOptions : : g_option_table [ ] =
{
2016-02-20 00:58:29 +00:00
{ LLDB_OPT_SET_ALL , false , " hide-aliases " , ' a ' , OptionParser : : eNoArgument , nullptr , nullptr , 0 , eArgTypeNone , " Hide aliases in the command list. " } ,
{ LLDB_OPT_SET_ALL , false , " hide-user-commands " , ' u ' , OptionParser : : eNoArgument , nullptr , nullptr , 0 , eArgTypeNone , " Hide user-defined commands from the list. " } ,
{ LLDB_OPT_SET_ALL , false , " show-hidden-commands " , ' h ' , OptionParser : : eNoArgument , nullptr , nullptr , 0 , eArgTypeNone , " Include commands prefixed with an underscore. " } ,
{ 0 , false , nullptr , 0 , 0 , nullptr , nullptr , 0 , eArgTypeNone , nullptr }
2011-09-09 17:49:36 +00:00
} ;
2010-06-08 16:52:24 +00:00
bool
2012-06-08 21:56:10 +00:00
CommandObjectHelp : : DoExecute ( Args & command , CommandReturnObject & result )
2010-06-08 16:52:24 +00:00
{
CommandObject : : CommandMap : : iterator pos ;
CommandObject * cmd_obj ;
2013-01-25 18:06:21 +00:00
const size_t argc = command . GetArgumentCount ( ) ;
2010-06-23 01:19:29 +00:00
2011-09-09 17:49:36 +00:00
// 'help' doesn't take any arguments, other than command names. If argc is 0, we show the user
// all commands (aliases and user commands if asked for). Otherwise every argument must be the name of a command or a sub-command.
2010-06-08 16:52:24 +00:00
if ( argc = = 0 )
{
2011-09-09 17:49:36 +00:00
uint32_t cmd_types = CommandInterpreter : : eCommandTypesBuiltin ;
if ( m_options . m_show_aliases )
cmd_types | = CommandInterpreter : : eCommandTypesAliases ;
if ( m_options . m_show_user_defined )
cmd_types | = CommandInterpreter : : eCommandTypesUserDef ;
2015-01-15 00:52:41 +00:00
if ( m_options . m_show_hidden )
cmd_types | = CommandInterpreter : : eCommandTypesHidden ;
2011-09-09 17:49:36 +00:00
2010-06-08 16:52:24 +00:00
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
2011-09-09 17:49:36 +00:00
m_interpreter . GetHelp ( result , cmd_types ) ; // General help
2010-06-08 16:52:24 +00:00
}
else
{
// Get command object for the first command argument. Only search built-in command dictionary.
2010-07-06 22:46:59 +00:00
StringList matches ;
2010-09-18 01:14:36 +00:00
cmd_obj = m_interpreter . GetCommandObject ( command . GetArgumentAtIndex ( 0 ) , & matches ) ;
2010-10-28 23:17:48 +00:00
bool is_alias_command = m_interpreter . AliasExists ( command . GetArgumentAtIndex ( 0 ) ) ;
std : : string alias_name = command . GetArgumentAtIndex ( 0 ) ;
2010-06-23 01:19:29 +00:00
2016-02-20 00:58:29 +00:00
if ( cmd_obj ! = nullptr )
2010-06-08 16:52:24 +00:00
{
2010-11-30 22:59:37 +00:00
StringList matches ;
2010-06-08 16:52:24 +00:00
bool all_okay = true ;
CommandObject * sub_cmd_obj = cmd_obj ;
// Loop down through sub_command dictionaries until we find the command object that corresponds
// to the help command entered.
2016-02-29 23:22:53 +00:00
std : : string sub_command ;
2013-06-19 19:04:53 +00:00
for ( size_t i = 1 ; i < argc & & all_okay ; + + i )
2010-06-08 16:52:24 +00:00
{
2016-02-29 23:22:53 +00:00
sub_command = command . GetArgumentAtIndex ( i ) ;
2010-11-30 22:59:37 +00:00
matches . Clear ( ) ;
2016-03-14 22:17:04 +00:00
if ( sub_cmd_obj - > IsAlias ( ) )
sub_cmd_obj = ( ( CommandAlias * ) sub_cmd_obj ) - > GetUnderlyingCommand ( ) . get ( ) ;
2010-06-08 16:52:24 +00:00
if ( ! sub_cmd_obj - > IsMultiwordObject ( ) )
{
all_okay = false ;
}
else
{
2010-11-30 22:59:37 +00:00
CommandObject * found_cmd ;
2012-10-13 02:07:45 +00:00
found_cmd = sub_cmd_obj - > GetSubcommandObject ( sub_command . c_str ( ) , & matches ) ;
2016-02-20 00:58:29 +00:00
if ( found_cmd = = nullptr )
2010-11-30 22:59:37 +00:00
all_okay = false ;
2010-12-01 00:42:17 +00:00
else if ( matches . GetSize ( ) > 1 )
2010-06-23 01:19:29 +00:00
all_okay = false ;
2010-11-30 22:59:37 +00:00
else
sub_cmd_obj = found_cmd ;
2010-06-08 16:52:24 +00:00
}
}
2010-06-23 01:19:29 +00:00
2016-02-20 00:58:29 +00:00
if ( ! all_okay | | ( sub_cmd_obj = = nullptr ) )
2010-06-08 16:52:24 +00:00
{
std : : string cmd_string ;
command . GetCommandString ( cmd_string ) ;
2013-06-12 01:50:57 +00:00
if ( matches . GetSize ( ) > = 2 )
2010-11-30 22:59:37 +00:00
{
StreamString s ;
s . Printf ( " ambiguous command %s " , cmd_string . c_str ( ) ) ;
size_t num_matches = matches . GetSize ( ) ;
for ( size_t match_idx = 0 ; match_idx < num_matches ; match_idx + + )
{
s . Printf ( " \n \t %s " , matches . GetStringAtIndex ( match_idx ) ) ;
}
s . Printf ( " \n " ) ;
result . AppendError ( s . GetData ( ) ) ;
2013-06-12 01:50:57 +00:00
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
2010-11-30 22:59:37 +00:00
}
2013-06-12 01:50:57 +00:00
else if ( ! sub_cmd_obj )
2010-06-08 16:52:24 +00:00
{
2016-02-29 23:22:53 +00:00
StreamString error_msg_stream ;
GenerateAdditionalHelpAvenuesMessage ( & error_msg_stream ,
cmd_string . c_str ( ) ,
m_interpreter . GetCommandPrefix ( ) ,
sub_command . c_str ( ) ) ;
result . AppendErrorWithFormat ( " %s " , error_msg_stream . GetData ( ) ) ;
2013-06-12 01:50:57 +00:00
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
2010-06-08 16:52:24 +00:00
}
else
{
2016-02-29 23:22:53 +00:00
GenerateAdditionalHelpAvenuesMessage ( & result . GetOutputStream ( ) ,
cmd_string . c_str ( ) ,
m_interpreter . GetCommandPrefix ( ) ,
sub_command . c_str ( ) ) ;
result . GetOutputStream ( ) . Printf ( " \n The closest match is '%s'. Help on it follows. \n \n " , sub_cmd_obj - > GetCommandName ( ) ) ;
2010-06-08 16:52:24 +00:00
}
}
2010-10-28 23:17:48 +00:00
2013-06-12 01:50:57 +00:00
sub_cmd_obj - > GenerateHelpText ( result ) ;
2010-10-28 23:17:48 +00:00
if ( is_alias_command )
{
StreamString sstr ;
Last round of preliminary cleanup in my refactoring of aliases.
The next step is to actually turn CommandAlias into a full-blown CommandObject citizen.
This is tricky given the current architecture of the CommandInterpreter but I think I have found a reasonable path forward.
The current plan is to make class CommandAlias : public CommandObject, and have all the several GetCommand calls not actually traverse through the alias to the underlying command object
The only times that an alias will be traversed are:
a) execution; when time comes to run an alias, I will just grab the underlying command and options, and make the interpreter execute that according to its current algorithm
b) subcommand traversal; if one has an alias to a multiword command, grabbing a subcommand will see through to the subcommand
Other operations, e.g. command listing, command names, command helps, ..., will all use the alias directly. This will, in turn, lead to the removal of the separate alias dictionary, and just mix user commands and aliases in one map
llvm-svn: 262986
2016-03-09 02:27:57 +00:00
m_interpreter . GetAlias ( alias_name . c_str ( ) ) - > GetAliasExpansion ( sstr ) ;
2010-10-28 23:17:48 +00:00
result . GetOutputStream ( ) . Printf ( " \n '%s' is an abbreviation for %s \n " , alias_name . c_str ( ) , sstr . GetData ( ) ) ;
}
2010-06-08 16:52:24 +00:00
}
2010-07-06 22:46:59 +00:00
else if ( matches . GetSize ( ) > 0 )
{
Stream & output_strm = result . GetOutputStream ( ) ;
output_strm . Printf ( " Help requested with ambiguous command name, possible completions: \n " ) ;
2013-01-25 18:06:21 +00:00
const size_t match_count = matches . GetSize ( ) ;
for ( size_t i = 0 ; i < match_count ; i + + )
2010-07-06 22:46:59 +00:00
{
output_strm . Printf ( " \t %s \n " , matches . GetStringAtIndex ( i ) ) ;
}
}
2010-06-08 16:52:24 +00:00
else
{
Add infrastructure for standardizing arguments for commands and
command options; makes it easier to ensure that the same type of
argument will have the same name everywhere, hooks up help for command
arguments, so that users can ask for help when they are confused about
what an argument should be; puts in the beginnings of the ability to
do tab-completion for certain types of arguments, allows automatic
syntax help generation for commands with arguments, and adds command
arguments into command options help correctly.
Currently only the breakpoint-id and breakpoint-id-range arguments, in
the breakpoint commands, have been hooked up to use the new mechanism.
The next steps will be to fix the command options arguments to use
this mechanism, and to fix the rest of the regular command arguments
to use this mechanism. Most of the help text is currently missing or
dummy text; this will need to be filled in, and the existing argument
help text will need to be cleaned up a bit (it was thrown in quickly,
mostly for testing purposes).
Help command now works for all argument types, although the help may not
be very helpful yet.
Those commands that take "raw" command strings now indicate it in their
help text.
llvm-svn: 115318
2010-10-01 17:46:38 +00:00
// Maybe the user is asking for help about a command argument rather than a command.
const CommandArgumentType arg_type = CommandObject : : LookupArgumentName ( command . GetArgumentAtIndex ( 0 ) ) ;
if ( arg_type ! = eArgTypeLastArg )
{
Stream & output_strm = result . GetOutputStream ( ) ;
CommandObject : : GetArgumentHelp ( output_strm , arg_type , m_interpreter ) ;
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
}
else
{
2016-02-29 23:22:53 +00:00
StreamString error_msg_stream ;
GenerateAdditionalHelpAvenuesMessage ( & error_msg_stream , command . GetArgumentAtIndex ( 0 ) , m_interpreter . GetCommandPrefix ( ) ) ;
result . AppendErrorWithFormat ( " %s " , error_msg_stream . GetData ( ) ) ;
Add infrastructure for standardizing arguments for commands and
command options; makes it easier to ensure that the same type of
argument will have the same name everywhere, hooks up help for command
arguments, so that users can ask for help when they are confused about
what an argument should be; puts in the beginnings of the ability to
do tab-completion for certain types of arguments, allows automatic
syntax help generation for commands with arguments, and adds command
arguments into command options help correctly.
Currently only the breakpoint-id and breakpoint-id-range arguments, in
the breakpoint commands, have been hooked up to use the new mechanism.
The next steps will be to fix the command options arguments to use
this mechanism, and to fix the rest of the regular command arguments
to use this mechanism. Most of the help text is currently missing or
dummy text; this will need to be filled in, and the existing argument
help text will need to be cleaned up a bit (it was thrown in quickly,
mostly for testing purposes).
Help command now works for all argument types, although the help may not
be very helpful yet.
Those commands that take "raw" command strings now indicate it in their
help text.
llvm-svn: 115318
2010-10-01 17:46:38 +00:00
result . SetStatus ( eReturnStatusFailed ) ;
}
2010-06-08 16:52:24 +00:00
}
}
2010-06-23 01:19:29 +00:00
2010-06-08 16:52:24 +00:00
return result . Succeeded ( ) ;
}
int
2016-02-20 00:58:29 +00:00
CommandObjectHelp : : HandleCompletion ( Args & input ,
int & cursor_index ,
int & cursor_char_position ,
int match_start_point ,
int max_return_elements ,
bool & word_complete ,
StringList & matches )
2010-06-08 16:52:24 +00:00
{
// Return the completions of the commands in the help system:
if ( cursor_index = = 0 )
{
2010-09-18 01:14:36 +00:00
return m_interpreter . HandleCompletionMatches ( input ,
cursor_index ,
cursor_char_position ,
match_start_point ,
max_return_elements ,
word_complete ,
matches ) ;
2010-06-08 16:52:24 +00:00
}
else
{
2010-09-18 01:14:36 +00:00
CommandObject * cmd_obj = m_interpreter . GetCommandObject ( input . GetArgumentAtIndex ( 0 ) ) ;
2011-10-26 19:32:01 +00:00
// The command that they are getting help on might be ambiguous, in which case we should complete that,
// otherwise complete with the command the user is getting help on...
if ( cmd_obj )
{
input . Shift ( ) ;
cursor_index - - ;
return cmd_obj - > HandleCompletion ( input ,
cursor_index ,
cursor_char_position ,
match_start_point ,
max_return_elements ,
word_complete ,
matches ) ;
}
else
{
return m_interpreter . HandleCompletionMatches ( input ,
cursor_index ,
cursor_char_position ,
match_start_point ,
max_return_elements ,
word_complete ,
matches ) ;
}
2010-06-08 16:52:24 +00:00
}
}