2010-06-09 00:52:24 +08: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 08:58:29 +08:00
# include "CommandObjectHelp.h"
2010-06-09 00:52:24 +08:00
# include "lldb/Interpreter/CommandInterpreter.h"
2016-09-07 04:57:50 +08:00
# include "lldb/Interpreter/CommandObjectMultiword.h"
2010-06-09 00:52:24 +08:00
# include "lldb/Interpreter/CommandReturnObject.h"
2016-09-07 04:57:50 +08:00
# include "lldb/Interpreter/Options.h"
2010-06-09 00:52:24 +08:00
using namespace lldb ;
using namespace lldb_private ;
//-------------------------------------------------------------------------
// CommandObjectHelp
//-------------------------------------------------------------------------
2016-09-07 04:57:50 +08:00
void CommandObjectHelp : : GenerateAdditionalHelpAvenuesMessage (
2016-11-17 05:34:22 +08:00
Stream * s , llvm : : StringRef command , llvm : : StringRef prefix , llvm : : StringRef subcommand ,
2016-09-07 04:57:50 +08:00
bool include_apropos , bool include_type_lookup ) {
2016-11-17 05:34:22 +08:00
if ( ! s | | command . empty ( ) )
return ;
std : : string command_str = command . str ( ) ;
std : : string prefix_str = prefix . str ( ) ;
std : : string subcommand_str = subcommand . str ( ) ;
const std : : string & lookup_str = ! subcommand_str . empty ( ) ? subcommand_str : command_str ;
s - > Printf ( " '%s' is not a known command. \n " , command_str . c_str ( ) ) ;
s - > Printf ( " Try '%shelp' to see a current list of commands. \n " ,
prefix . str ( ) . c_str ( ) ) ;
if ( include_apropos ) {
s - > Printf ( " Try '%sapropos %s' for a list of related commands. \n " ,
prefix_str . c_str ( ) , lookup_str . c_str ( ) ) ;
}
if ( include_type_lookup ) {
s - > Printf ( " Try '%stype lookup %s' for information on types, methods, "
" functions, modules, etc. " ,
prefix_str . c_str ( ) , lookup_str . c_str ( ) ) ;
2016-09-07 04:57:50 +08:00
}
2016-03-01 07:22:53 +08:00
}
2016-07-15 06:03:10 +08:00
CommandObjectHelp : : CommandObjectHelp ( CommandInterpreter & interpreter )
2016-09-07 04:57:50 +08:00
: CommandObjectParsed ( interpreter , " help " , " Show a list of all debugger "
" commands, or give details "
" about a specific command. " ,
2016-07-15 06:03:10 +08:00
" help [<cmd-name>] " ) ,
2016-09-07 04:57:50 +08:00
m_options ( ) {
CommandArgumentEntry arg ;
CommandArgumentData command_arg ;
2010-10-05 06:28:36 +08:00
2016-09-07 04:57:50 +08:00
// Define the first (and only) variant of this arg.
command_arg . arg_type = eArgTypeCommandName ;
command_arg . arg_repetition = eArgRepeatStar ;
2010-10-05 06:28:36 +08:00
2016-09-07 04:57:50 +08:00
// There is only one variant this argument could be; put it into the argument
// entry.
arg . push_back ( command_arg ) ;
2010-10-05 06:28:36 +08:00
2016-09-07 04:57:50 +08:00
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg ) ;
2010-06-09 00:52:24 +08:00
}
2016-02-20 08:58:29 +08:00
CommandObjectHelp : : ~ CommandObjectHelp ( ) = default ;
2010-06-09 00:52:24 +08:00
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
static OptionDefinition g_help_options [ ] = {
2016-09-07 04:57:50 +08:00
// clang-format off
2016-08-27 07:28:47 +08: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. " } ,
2016-09-07 04:57:50 +08:00
// clang-format on
2011-09-10 01:49:36 +08:00
} ;
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
llvm : : ArrayRef < OptionDefinition >
CommandObjectHelp : : CommandOptions : : GetDefinitions ( ) {
2016-09-23 05:06:13 +08:00
return llvm : : makeArrayRef ( g_help_options ) ;
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
}
2016-09-07 04:57:50 +08:00
bool CommandObjectHelp : : DoExecute ( Args & command , CommandReturnObject & result ) {
CommandObject : : CommandMap : : iterator pos ;
CommandObject * cmd_obj ;
const size_t argc = command . GetArgumentCount ( ) ;
2011-09-10 01:49:36 +08:00
2018-05-01 00:49:04 +08: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.
2016-09-07 04:57:50 +08:00
if ( argc = = 0 ) {
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 ;
if ( m_options . m_show_hidden )
cmd_types | = CommandInterpreter : : eCommandTypesHidden ;
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
m_interpreter . GetHelp ( result , cmd_types ) ; // General help
} else {
// Get command object for the first command argument. Only search built-in
// command dictionary.
StringList matches ;
2016-12-09 09:08:29 +08:00
auto command_name = command [ 0 ] . ref ;
cmd_obj = m_interpreter . GetCommandObject ( command_name , & matches ) ;
2016-09-07 04:57:50 +08:00
if ( cmd_obj ! = nullptr ) {
StringList matches ;
bool all_okay = true ;
CommandObject * sub_cmd_obj = cmd_obj ;
// Loop down through sub_command dictionaries until we find the command
2016-10-06 07:40:23 +08:00
// object that corresponds to the help command entered.
2016-09-07 04:57:50 +08:00
std : : string sub_command ;
2016-10-06 07:40:23 +08:00
for ( auto & entry : command . entries ( ) . drop_front ( ) ) {
sub_command = entry . ref ;
2016-09-07 04:57:50 +08:00
matches . Clear ( ) ;
if ( sub_cmd_obj - > IsAlias ( ) )
sub_cmd_obj =
( ( CommandAlias * ) sub_cmd_obj ) - > GetUnderlyingCommand ( ) . get ( ) ;
if ( ! sub_cmd_obj - > IsMultiwordObject ( ) ) {
all_okay = false ;
2016-10-06 07:40:23 +08:00
break ;
2016-09-07 04:57:50 +08:00
} else {
CommandObject * found_cmd ;
found_cmd =
sub_cmd_obj - > GetSubcommandObject ( sub_command . c_str ( ) , & matches ) ;
2016-10-06 07:40:23 +08:00
if ( found_cmd = = nullptr | | matches . GetSize ( ) > 1 ) {
2016-09-07 04:57:50 +08:00
all_okay = false ;
2016-10-06 07:40:23 +08:00
break ;
} else
2016-09-07 04:57:50 +08:00
sub_cmd_obj = found_cmd ;
2010-07-07 06:46:59 +08:00
}
2016-09-07 04:57:50 +08:00
}
if ( ! all_okay | | ( sub_cmd_obj = = nullptr ) ) {
std : : string cmd_string ;
command . GetCommandString ( cmd_string ) ;
if ( matches . GetSize ( ) > = 2 ) {
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 " ) ;
2016-11-17 05:15:24 +08:00
result . AppendError ( s . GetString ( ) ) ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
} else if ( ! sub_cmd_obj ) {
StreamString error_msg_stream ;
GenerateAdditionalHelpAvenuesMessage (
& error_msg_stream , cmd_string . c_str ( ) ,
m_interpreter . GetCommandPrefix ( ) , sub_command . c_str ( ) ) ;
2016-11-17 05:15:24 +08:00
result . AppendError ( error_msg_stream . GetString ( ) ) ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
} else {
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 " ,
2016-10-06 05:14:38 +08:00
sub_cmd_obj - > GetCommandName ( ) . str ( ) . c_str ( ) ) ;
2010-06-09 00:52:24 +08:00
}
2016-09-07 04:57:50 +08:00
}
sub_cmd_obj - > GenerateHelpText ( result ) ;
2016-12-09 09:08:29 +08:00
if ( m_interpreter . AliasExists ( command_name ) ) {
2016-09-07 04:57:50 +08:00
StreamString sstr ;
2016-12-09 09:08:29 +08:00
m_interpreter . GetAlias ( command_name ) - > GetAliasExpansion ( sstr ) ;
2016-09-07 04:57:50 +08:00
result . GetOutputStream ( ) . Printf ( " \n '%s' is an abbreviation for %s \n " ,
2016-12-09 09:08:29 +08:00
command [ 0 ] . c_str ( ) , sstr . GetData ( ) ) ;
2016-09-07 04:57:50 +08:00
}
} else if ( matches . GetSize ( ) > 0 ) {
Stream & output_strm = result . GetOutputStream ( ) ;
output_strm . Printf ( " Help requested with ambiguous command name, possible "
" completions: \n " ) ;
const size_t match_count = matches . GetSize ( ) ;
for ( size_t i = 0 ; i < match_count ; i + + ) {
output_strm . Printf ( " \t %s \n " , matches . GetStringAtIndex ( i ) ) ;
}
} else {
// Maybe the user is asking for help about a command argument rather than
// a command.
const CommandArgumentType arg_type =
2016-12-09 09:08:29 +08:00
CommandObject : : LookupArgumentName ( command_name ) ;
2016-09-07 04:57:50 +08:00
if ( arg_type ! = eArgTypeLastArg ) {
Stream & output_strm = result . GetOutputStream ( ) ;
CommandObject : : GetArgumentHelp ( output_strm , arg_type , m_interpreter ) ;
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
} else {
StreamString error_msg_stream ;
2016-12-09 09:08:29 +08:00
GenerateAdditionalHelpAvenuesMessage ( & error_msg_stream , command_name ,
m_interpreter . GetCommandPrefix ( ) ,
" " ) ;
2016-11-17 05:15:24 +08:00
result . AppendError ( error_msg_stream . GetString ( ) ) ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
}
2010-06-09 00:52:24 +08:00
}
2016-09-07 04:57:50 +08:00
}
return result . Succeeded ( ) ;
2010-06-09 00:52:24 +08:00
}
2016-09-07 04:57:50 +08:00
int CommandObjectHelp : : HandleCompletion ( Args & input , int & cursor_index ,
int & cursor_char_position ,
int match_start_point ,
int max_return_elements ,
bool & word_complete ,
StringList & matches ) {
// Return the completions of the commands in the help system:
if ( cursor_index = = 0 ) {
return m_interpreter . HandleCompletionMatches (
input , cursor_index , cursor_char_position , match_start_point ,
max_return_elements , word_complete , matches ) ;
} else {
2016-12-09 09:08:29 +08:00
CommandObject * cmd_obj = m_interpreter . GetCommandObject ( input [ 0 ] . ref ) ;
2016-09-07 04:57:50 +08:00
// The command that they are getting help on might be ambiguous, in which
2018-05-01 00:49:04 +08:00
// case we should complete that, otherwise complete with the command the
// user is getting help on...
2016-09-07 04:57:50 +08:00
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-09 00:52:24 +08:00
}
2016-09-07 04:57:50 +08:00
}
2010-06-09 00:52:24 +08:00
}