2010-06-09 00:52:24 +08:00
//===-- Options.cpp ---------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
2012-12-05 08:20:57 +08:00
# include "lldb/lldb-python.h"
2010-06-16 03:49:27 +08:00
# include "lldb/Interpreter/Options.h"
2010-06-09 00:52:24 +08:00
// C Includes
// C++ Includes
2010-09-10 00:44:14 +08:00
# include <algorithm>
2011-04-14 06:47:15 +08:00
# include <bitset>
2012-12-04 08:32:51 +08:00
# include <map>
2010-06-09 00:52:24 +08:00
// Other libraries and framework includes
// Project includes
# include "lldb/Interpreter/CommandObject.h"
# include "lldb/Interpreter/CommandReturnObject.h"
# include "lldb/Interpreter/CommandCompletions.h"
# include "lldb/Interpreter/CommandInterpreter.h"
# include "lldb/Core/StreamString.h"
# include "lldb/Target/Target.h"
using namespace lldb ;
using namespace lldb_private ;
//-------------------------------------------------------------------------
// Options
//-------------------------------------------------------------------------
2011-04-08 06:46:35 +08:00
Options : : Options ( CommandInterpreter & interpreter ) :
m_interpreter ( interpreter ) ,
2010-06-09 00:52:24 +08:00
m_getopt_table ( )
{
2010-06-16 02:47:14 +08:00
BuildValidOptionSets ( ) ;
2010-06-09 00:52:24 +08:00
}
Options : : ~ Options ( )
{
}
void
2011-04-13 08:18:08 +08:00
Options : : NotifyOptionParsingStarting ( )
2010-06-09 00:52:24 +08:00
{
m_seen_options . clear ( ) ;
Many improvements to the Platform base class and subclasses. The base Platform
class now implements the Host functionality for a lot of things that make
sense by default so that subclasses can check:
int
PlatformSubclass::Foo ()
{
if (IsHost())
return Platform::Foo (); // Let the platform base class do the host specific stuff
// Platform subclass specific code...
int result = ...
return result;
}
Added new functions to the platform:
virtual const char *Platform::GetUserName (uint32_t uid);
virtual const char *Platform::GetGroupName (uint32_t gid);
The user and group names are cached locally so that remote platforms can avoid
sending packets multiple times to resolve this information.
Added the parent process ID to the ProcessInfo class.
Added a new ProcessInfoMatch class which helps us to match processes up
and changed the Host layer over to using this new class. The new class allows
us to search for processs:
1 - by name (equal to, starts with, ends with, contains, and regex)
2 - by pid
3 - And further check for parent pid == value, uid == value, gid == value,
euid == value, egid == value, arch == value, parent == value.
This is all hookup up to the "platform process list" command which required
adding dumping routines to dump process information. If the Host class
implements the process lookup routines, you can now lists processes on
your local machine:
machine1.foo.com % lldb
(lldb) platform process list
PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE NAME
====== ====== ========== ========== ========== ========== ======================== ============================
99538 1 username usergroup username usergroup x86_64-apple-darwin FileMerge
94943 1 username usergroup username usergroup x86_64-apple-darwin mdworker
94852 244 username usergroup username usergroup x86_64-apple-darwin Safari
94727 244 username usergroup username usergroup x86_64-apple-darwin Xcode
92742 92710 username usergroup username usergroup i386-apple-darwin debugserver
This of course also works remotely with the lldb-platform:
machine1.foo.com % lldb-platform --listen 1234
machine2.foo.com % lldb
(lldb) platform create remote-macosx
Platform: remote-macosx
Connected: no
(lldb) platform connect connect://localhost:1444
Platform: remote-macosx
Triple: x86_64-apple-darwin
OS Version: 10.6.7 (10J869)
Kernel: Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386
Hostname: machine1.foo.com
Connected: yes
(lldb) platform process list
PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE NAME
====== ====== ========== ========== ========== ========== ======================== ============================
99556 244 username usergroup username usergroup x86_64-apple-darwin trustevaluation
99548 65539 username usergroup username usergroup x86_64-apple-darwin lldb
99538 1 username usergroup username usergroup x86_64-apple-darwin FileMerge
94943 1 username usergroup username usergroup x86_64-apple-darwin mdworker
94852 244 username usergroup username usergroup x86_64-apple-darwin Safari
The lldb-platform implements everything with the Host:: layer, so this should
"just work" for linux. I will probably be adding more stuff to the Host layer
for launching processes and attaching to processes so that this support should
eventually just work as well.
Modified the target to be able to be created with an architecture that differs
from the main executable. This is needed for iOS debugging since we can have
an "armv6" binary which can run on an "armv7" machine, so we want to be able
to do:
% lldb
(lldb) platform create remote-ios
(lldb) file --arch armv7 a.out
Where "a.out" is an armv6 executable. The platform then can correctly decide
to open all "armv7" images for all dependent shared libraries.
Modified the disassembly to show the current PC value. Example output:
(lldb) disassemble --frame
a.out`main:
0x1eb7: pushl %ebp
0x1eb8: movl %esp, %ebp
0x1eba: pushl %ebx
0x1ebb: subl $20, %esp
0x1ebe: calll 0x1ec3 ; main + 12 at test.c:18
0x1ec3: popl %ebx
-> 0x1ec4: calll 0x1f12 ; getpid
0x1ec9: movl %eax, 4(%esp)
0x1ecd: leal 199(%ebx), %eax
0x1ed3: movl %eax, (%esp)
0x1ed6: calll 0x1f18 ; printf
0x1edb: leal 213(%ebx), %eax
0x1ee1: movl %eax, (%esp)
0x1ee4: calll 0x1f1e ; puts
0x1ee9: calll 0x1f0c ; getchar
0x1eee: movl $20, (%esp)
0x1ef5: calll 0x1e6a ; sleep_loop at test.c:6
0x1efa: movl $12, %eax
0x1eff: addl $20, %esp
0x1f02: popl %ebx
0x1f03: leave
0x1f04: ret
This can be handy when dealing with the new --line options that was recently
added:
(lldb) disassemble --line
a.out`main + 13 at test.c:19
18 {
-> 19 printf("Process: %i\n\n", getpid());
20 puts("Press any key to continue..."); getchar();
-> 0x1ec4: calll 0x1f12 ; getpid
0x1ec9: movl %eax, 4(%esp)
0x1ecd: leal 199(%ebx), %eax
0x1ed3: movl %eax, (%esp)
0x1ed6: calll 0x1f18 ; printf
Modified the ModuleList to have a lookup based solely on a UUID. Since the
UUID is typically the MD5 checksum of a binary image, there is no need
to give the path and architecture when searching for a pre-existing
image in an image list.
Now that we support remote debugging a bit better, our lldb_private::Module
needs to be able to track what the original path for file was as the platform
knows it, as well as where the file is locally. The module has the two
following functions to retrieve both paths:
const FileSpec &Module::GetFileSpec () const;
const FileSpec &Module::GetPlatformFileSpec () const;
llvm-svn: 128563
2011-03-31 02:16:51 +08:00
// Let the subclass reset its option values
2011-04-13 08:18:08 +08:00
OptionParsingStarting ( ) ;
}
Error
Options : : NotifyOptionParsingFinished ( )
{
return OptionParsingFinished ( ) ;
2010-06-09 00:52:24 +08:00
}
void
Options : : OptionSeen ( int option_idx )
{
2012-12-04 08:32:51 +08:00
m_seen_options . insert ( option_idx ) ;
2010-06-09 00:52:24 +08:00
}
// Returns true is set_a is a subset of set_b; Otherwise returns false.
bool
Options : : IsASubset ( const OptionSet & set_a , const OptionSet & set_b )
{
bool is_a_subset = true ;
OptionSet : : const_iterator pos_a ;
OptionSet : : const_iterator pos_b ;
// set_a is a subset of set_b if every member of set_a is also a member of set_b
for ( pos_a = set_a . begin ( ) ; pos_a ! = set_a . end ( ) & & is_a_subset ; + + pos_a )
{
pos_b = set_b . find ( * pos_a ) ;
if ( pos_b = = set_b . end ( ) )
is_a_subset = false ;
}
return is_a_subset ;
}
// Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) && !ElementOf (x, set_b) }
size_t
Options : : OptionsSetDiff ( const OptionSet & set_a , const OptionSet & set_b , OptionSet & diffs )
{
size_t num_diffs = 0 ;
OptionSet : : const_iterator pos_a ;
OptionSet : : const_iterator pos_b ;
for ( pos_a = set_a . begin ( ) ; pos_a ! = set_a . end ( ) ; + + pos_a )
{
pos_b = set_b . find ( * pos_a ) ;
if ( pos_b = = set_b . end ( ) )
{
+ + num_diffs ;
diffs . insert ( * pos_a ) ;
}
}
return num_diffs ;
}
// Returns the union of set_a and set_b. Does not put duplicate members into the union.
void
Options : : OptionsSetUnion ( const OptionSet & set_a , const OptionSet & set_b , OptionSet & union_set )
{
OptionSet : : const_iterator pos ;
OptionSet : : iterator pos_union ;
// Put all the elements of set_a into the union.
for ( pos = set_a . begin ( ) ; pos ! = set_a . end ( ) ; + + pos )
union_set . insert ( * pos ) ;
// Put all the elements of set_b that are not already there into the union.
for ( pos = set_b . begin ( ) ; pos ! = set_b . end ( ) ; + + pos )
{
pos_union = union_set . find ( * pos ) ;
if ( pos_union = = union_set . end ( ) )
union_set . insert ( * pos ) ;
}
}
bool
Options : : VerifyOptions ( CommandReturnObject & result )
{
bool options_are_valid = false ;
2010-06-25 04:30:15 +08:00
int num_levels = GetRequiredOptions ( ) . size ( ) ;
2010-06-09 00:52:24 +08:00
if ( num_levels )
{
for ( int i = 0 ; i < num_levels & & ! options_are_valid ; + + i )
{
// This is the correct set of options if: 1). m_seen_options contains all of m_required_options[i]
// (i.e. all the required options at this level are a subset of m_seen_options); AND
// 2). { m_seen_options - m_required_options[i] is a subset of m_options_options[i] (i.e. all the rest of
// m_seen_options are in the set of optional options at this level.
// Check to see if all of m_required_options[i] are a subset of m_seen_options
2010-06-25 04:30:15 +08:00
if ( IsASubset ( GetRequiredOptions ( ) [ i ] , m_seen_options ) )
2010-06-09 00:52:24 +08:00
{
// Construct the set difference: remaining_options = {m_seen_options} - {m_required_options[i]}
OptionSet remaining_options ;
2010-06-25 04:30:15 +08:00
OptionsSetDiff ( m_seen_options , GetRequiredOptions ( ) [ i ] , remaining_options ) ;
2010-06-09 00:52:24 +08:00
// Check to see if remaining_options is a subset of m_optional_options[i]
2010-06-25 04:30:15 +08:00
if ( IsASubset ( remaining_options , GetOptionalOptions ( ) [ i ] ) )
2010-06-09 00:52:24 +08:00
options_are_valid = true ;
}
}
}
else
{
options_are_valid = true ;
}
if ( options_are_valid )
{
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
}
else
{
result . AppendError ( " invalid combination of options for the given command " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
}
return options_are_valid ;
}
2010-06-16 02:47:14 +08:00
// This is called in the Options constructor, though we could call it lazily if that ends up being
// a performance problem.
2010-06-09 00:52:24 +08:00
void
Options : : BuildValidOptionSets ( )
{
// Check to see if we already did this.
if ( m_required_options . size ( ) ! = 0 )
return ;
// Check to see if there are any options.
int num_options = NumCommandOptions ( ) ;
if ( num_options = = 0 )
return ;
2011-10-29 08:57:28 +08:00
const OptionDefinition * opt_defs = GetDefinitions ( ) ;
2010-06-09 00:52:24 +08:00
m_required_options . resize ( 1 ) ;
m_optional_options . resize ( 1 ) ;
2010-06-16 02:47:14 +08:00
// First count the number of option sets we've got. Ignore LLDB_ALL_OPTION_SETS...
uint32_t num_option_sets = 0 ;
for ( int i = 0 ; i < num_options ; i + + )
2010-06-09 00:52:24 +08:00
{
2011-10-29 08:57:28 +08:00
uint32_t this_usage_mask = opt_defs [ i ] . usage_mask ;
2010-06-16 02:47:14 +08:00
if ( this_usage_mask = = LLDB_OPT_SET_ALL )
2010-06-09 00:52:24 +08:00
{
2010-06-16 02:47:14 +08:00
if ( num_option_sets = = 0 )
num_option_sets = 1 ;
2010-06-09 00:52:24 +08:00
}
else
{
2010-06-16 02:47:14 +08:00
for ( int j = 0 ; j < LLDB_MAX_NUM_OPTION_SETS ; j + + )
{
2010-06-25 04:30:15 +08:00
if ( this_usage_mask & ( 1 < < j ) )
2010-06-16 02:47:14 +08:00
{
if ( num_option_sets < = j )
num_option_sets = j + 1 ;
}
}
2010-06-09 00:52:24 +08:00
}
2010-06-16 02:47:14 +08:00
}
2010-06-09 00:52:24 +08:00
2010-06-16 02:47:14 +08:00
if ( num_option_sets > 0 )
{
m_required_options . resize ( num_option_sets ) ;
m_optional_options . resize ( num_option_sets ) ;
for ( int i = 0 ; i < num_options ; + + i )
{
for ( int j = 0 ; j < num_option_sets ; j + + )
{
2011-10-29 08:57:28 +08:00
if ( opt_defs [ i ] . usage_mask & 1 < < j )
2010-06-16 02:47:14 +08:00
{
2011-10-29 08:57:28 +08:00
if ( opt_defs [ i ] . required )
m_required_options [ j ] . insert ( opt_defs [ i ] . short_option ) ;
2010-06-16 02:47:14 +08:00
else
2011-10-29 08:57:28 +08:00
m_optional_options [ j ] . insert ( opt_defs [ i ] . short_option ) ;
2010-06-16 02:47:14 +08:00
}
}
}
2010-06-09 00:52:24 +08:00
}
}
uint32_t
Options : : NumCommandOptions ( )
{
2011-10-29 08:57:28 +08:00
const OptionDefinition * opt_defs = GetDefinitions ( ) ;
if ( opt_defs = = NULL )
2010-06-16 02:47:14 +08:00
return 0 ;
2010-06-09 00:52:24 +08:00
int i = 0 ;
2011-10-29 08:57:28 +08:00
if ( opt_defs ! = NULL )
2010-06-09 00:52:24 +08:00
{
2011-10-29 08:57:28 +08:00
while ( opt_defs [ i ] . long_option ! = NULL )
2010-06-09 00:52:24 +08:00
+ + i ;
}
return i ;
}
struct option *
Options : : GetLongOptions ( )
{
// Check to see if this has already been done.
if ( m_getopt_table . empty ( ) )
{
// Check to see if there are any options.
const uint32_t num_options = NumCommandOptions ( ) ;
if ( num_options = = 0 )
return NULL ;
uint32_t i ;
2011-10-29 08:57:28 +08:00
const OptionDefinition * opt_defs = GetDefinitions ( ) ;
2010-06-09 00:52:24 +08:00
2012-12-04 08:32:51 +08:00
std : : map < int , uint32_t > option_seen ;
2010-06-09 00:52:24 +08:00
m_getopt_table . resize ( num_options + 1 ) ;
2012-12-04 08:32:51 +08:00
for ( i = 0 ; i < num_options ; + + i )
2010-06-09 00:52:24 +08:00
{
2012-12-04 08:32:51 +08:00
const int short_opt = opt_defs [ i ] . short_option ;
m_getopt_table [ i ] . name = opt_defs [ i ] . long_option ;
m_getopt_table [ i ] . has_arg = opt_defs [ i ] . option_has_arg ;
m_getopt_table [ i ] . flag = NULL ;
m_getopt_table [ i ] . val = short_opt ;
2010-06-09 00:52:24 +08:00
2012-12-04 08:32:51 +08:00
if ( option_seen . find ( short_opt ) = = option_seen . end ( ) )
2010-06-09 00:52:24 +08:00
{
2012-12-04 08:32:51 +08:00
option_seen [ short_opt ] = i ;
2010-06-09 00:52:24 +08:00
}
2012-12-04 08:32:51 +08:00
else if ( short_opt )
2012-12-01 03:05:35 +08:00
{
2012-12-04 08:32:51 +08:00
m_getopt_table [ i ] . val = 0 ;
std : : map < int , uint32_t > : : const_iterator pos = option_seen . find ( short_opt ) ;
StreamString strm ;
2012-12-06 04:24:57 +08:00
if ( isprint8 ( short_opt ) )
2012-12-04 08:32:51 +08:00
Host : : SystemLog ( Host : : eSystemLogError , " option[%u] --%s has a short option -%c that conflicts with option[%u] --%s, short option won't be used for --%s \n " ,
i ,
opt_defs [ i ] . long_option ,
short_opt ,
pos - > second ,
m_getopt_table [ pos - > second ] . name ,
opt_defs [ i ] . long_option ) ;
else
Host : : SystemLog ( Host : : eSystemLogError , " option[%u] --%s has a short option 0x%x that conflicts with option[%u] --%s, short option won't be used for --%s \n " ,
i ,
opt_defs [ i ] . long_option ,
short_opt ,
pos - > second ,
m_getopt_table [ pos - > second ] . name ,
opt_defs [ i ] . long_option ) ;
2012-12-01 03:05:35 +08:00
}
2010-06-09 00:52:24 +08:00
}
2013-04-05 04:35:24 +08:00
//getopt_long_only requires a NULL final entry in the table:
2010-06-09 00:52:24 +08:00
2012-12-04 08:32:51 +08:00
m_getopt_table [ i ] . name = NULL ;
m_getopt_table [ i ] . has_arg = 0 ;
m_getopt_table [ i ] . flag = NULL ;
m_getopt_table [ i ] . val = 0 ;
2010-06-09 00:52:24 +08:00
}
2010-07-21 06:52:08 +08:00
if ( m_getopt_table . empty ( ) )
return NULL ;
return & m_getopt_table . front ( ) ;
2010-06-09 00:52:24 +08:00
}
// This function takes INDENT, which tells how many spaces to output at the front of each line; SPACES, which is
// a string containing 80 spaces; and TEXT, which is the text that is to be output. It outputs the text, on
// multiple lines if necessary, to RESULT, with INDENT spaces at the front of each line. It breaks lines on spaces,
// tabs or newlines, shortening the line if necessary to not break in the middle of a word. It assumes that each
// output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
void
Options : : OutputFormattedUsageText
(
Stream & strm ,
const char * text ,
uint32_t output_max_columns
)
{
int len = strlen ( text ) ;
// Will it all fit on one line?
if ( ( len + strm . GetIndentLevel ( ) ) < output_max_columns )
{
// Output it as a single line.
strm . Indent ( text ) ;
strm . EOL ( ) ;
}
else
{
// We need to break it up into multiple lines.
int text_width = output_max_columns - strm . GetIndentLevel ( ) - 1 ;
int start = 0 ;
int end = start ;
int final_end = strlen ( text ) ;
int sub_len ;
while ( end < final_end )
{
// Don't start the 'text' on a space, since we're already outputting the indentation.
while ( ( start < final_end ) & & ( text [ start ] = = ' ' ) )
start + + ;
end = start + text_width ;
if ( end > final_end )
end = final_end ;
else
{
// If we're not at the end of the text, make sure we break the line on white space.
while ( end > start
& & text [ end ] ! = ' ' & & text [ end ] ! = ' \t ' & & text [ end ] ! = ' \n ' )
end - - ;
}
sub_len = end - start ;
if ( start ! = 0 )
strm . EOL ( ) ;
strm . Indent ( ) ;
assert ( start < final_end ) ;
assert ( start + sub_len < = final_end ) ;
strm . Write ( text + start , sub_len ) ;
start = end + 1 ;
}
strm . EOL ( ) ;
}
}
2011-10-29 08:57:28 +08:00
bool
Options : : SupportsLongOption ( const char * long_option )
{
if ( long_option & & long_option [ 0 ] )
{
const OptionDefinition * opt_defs = GetDefinitions ( ) ;
if ( opt_defs )
{
2011-11-01 07:51:19 +08:00
const char * long_option_name = long_option ;
2011-10-29 08:57:28 +08:00
if ( long_option [ 0 ] = = ' - ' & & long_option [ 1 ] = = ' - ' )
long_option_name + = 2 ;
for ( uint32_t i = 0 ; opt_defs [ i ] . long_option ; + + i )
{
if ( strcmp ( opt_defs [ i ] . long_option , long_option_name ) = = 0 )
return true ;
}
}
}
return false ;
}
2012-12-04 08:32:51 +08:00
enum OptionDisplayType
{
eDisplayBestOption ,
eDisplayShortOption ,
eDisplayLongOption
} ;
static bool
PrintOption ( const OptionDefinition & opt_def ,
OptionDisplayType display_type ,
const char * header ,
const char * footer ,
bool show_optional ,
Stream & strm )
{
2012-12-06 04:24:57 +08:00
const bool has_short_option = isprint8 ( opt_def . short_option ) ! = 0 ;
2012-12-04 08:32:51 +08:00
if ( display_type = = eDisplayShortOption & & ! has_short_option )
return false ;
if ( header & & header [ 0 ] )
strm . PutCString ( header ) ;
if ( show_optional & & ! opt_def . required )
strm . PutChar ( ' [ ' ) ;
const bool show_short_option = has_short_option & & display_type ! = eDisplayLongOption ;
if ( show_short_option )
strm . Printf ( " -%c " , opt_def . short_option ) ;
else
strm . Printf ( " --%s " , opt_def . long_option ) ;
switch ( opt_def . option_has_arg )
{
case no_argument :
break ;
case required_argument :
strm . Printf ( " <%s> " , CommandObject : : GetArgumentName ( opt_def . argument_type ) ) ;
break ;
case optional_argument :
strm . Printf ( " %s[<%s>] " ,
show_short_option ? " " : " = " ,
CommandObject : : GetArgumentName ( opt_def . argument_type ) ) ;
break ;
}
if ( show_optional & & ! opt_def . required )
strm . PutChar ( ' ] ' ) ;
if ( footer & & footer [ 0 ] )
strm . PutCString ( footer ) ;
return true ;
}
2010-06-09 00:52:24 +08:00
void
Options : : GenerateOptionUsage
(
Stream & strm ,
2010-09-18 09:14:36 +08:00
CommandObject * cmd
)
2010-06-09 00:52:24 +08:00
{
2011-04-08 06:46:35 +08:00
const uint32_t screen_width = m_interpreter . GetDebugger ( ) . GetTerminalWidth ( ) ;
2010-09-04 08:03:46 +08:00
2011-10-29 08:57:28 +08:00
const OptionDefinition * opt_defs = GetDefinitions ( ) ;
2010-06-09 00:52:24 +08:00
const uint32_t save_indent_level = strm . GetIndentLevel ( ) ;
const char * name ;
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-02 01:46:38 +08:00
StreamString arguments_str ;
2010-06-09 00:52:24 +08:00
if ( cmd )
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-02 01:46:38 +08:00
{
2010-09-18 09:14:36 +08:00
name = cmd - > GetCommandName ( ) ;
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-02 01:46:38 +08:00
cmd - > GetFormattedCommandArguments ( arguments_str ) ;
}
2010-06-09 00:52:24 +08:00
else
2010-09-18 09:14:36 +08:00
name = " " ;
2010-06-09 00:52:24 +08:00
strm . PutCString ( " \n Command Options Usage: \n " ) ;
strm . IndentMore ( 2 ) ;
// First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
// <cmd> [options-for-level-1]
// etc.
const uint32_t num_options = NumCommandOptions ( ) ;
2010-06-16 02:47:14 +08:00
if ( num_options = = 0 )
return ;
2010-06-25 04:30:15 +08:00
int num_option_sets = GetRequiredOptions ( ) . size ( ) ;
2010-06-16 02:47:14 +08:00
2010-06-09 00:52:24 +08:00
uint32_t i ;
2010-06-16 02:47:14 +08:00
for ( uint32_t opt_set = 0 ; opt_set < num_option_sets ; + + opt_set )
2010-06-09 00:52:24 +08:00
{
2010-06-16 02:47:14 +08:00
uint32_t opt_set_mask ;
opt_set_mask = 1 < < opt_set ;
if ( opt_set > 0 )
strm . Printf ( " \n " ) ;
strm . Indent ( name ) ;
2010-09-10 00:44:14 +08:00
2012-02-08 09:13:31 +08:00
// Different option sets may require different args.
StreamString args_str ;
2012-10-13 01:34:26 +08:00
if ( cmd )
cmd - > GetFormattedCommandArguments ( args_str , opt_set_mask ) ;
2012-02-08 09:13:31 +08:00
2010-09-18 11:37:20 +08:00
// First go through and print all options that take no arguments as
// a single string. If a command has "-a" "-b" and "-c", this will show
// up as [-abc]
2012-12-04 08:32:51 +08:00
std : : set < int > options ;
std : : set < int > : : const_iterator options_pos , options_end ;
2013-04-20 05:31:16 +08:00
for ( i = 0 ; i < num_options ; + + i )
2010-09-18 11:37:20 +08:00
{
2012-12-06 04:24:57 +08:00
if ( opt_defs [ i ] . usage_mask & opt_set_mask & & isprint8 ( opt_defs [ i ] . short_option ) )
2010-09-18 11:37:20 +08:00
{
// Add current option to the end of out_stream.
2011-10-29 08:57:28 +08:00
if ( opt_defs [ i ] . required = = true & &
opt_defs [ i ] . option_has_arg = = no_argument )
2010-09-18 11:37:20 +08:00
{
2011-10-29 08:57:28 +08:00
options . insert ( opt_defs [ i ] . short_option ) ;
2010-09-18 11:37:20 +08:00
}
}
}
if ( options . empty ( ) = = false )
{
// We have some required options with no arguments
strm . PutCString ( " - " ) ;
for ( i = 0 ; i < 2 ; + + i )
for ( options_pos = options . begin ( ) , options_end = options . end ( ) ;
options_pos ! = options_end ;
+ + options_pos )
{
2012-12-04 08:32:51 +08:00
if ( i = = 0 & & : : islower ( * options_pos ) )
2010-09-18 11:37:20 +08:00
continue ;
2012-12-04 08:32:51 +08:00
if ( i = = 1 & & : : isupper ( * options_pos ) )
2010-09-18 11:37:20 +08:00
continue ;
2012-12-04 08:32:51 +08:00
strm < < ( char ) * options_pos ;
2010-09-18 11:37:20 +08:00
}
}
for ( i = 0 , options . clear ( ) ; i < num_options ; + + i )
{
2012-12-06 04:24:57 +08:00
if ( opt_defs [ i ] . usage_mask & opt_set_mask & & isprint8 ( opt_defs [ i ] . short_option ) )
2010-09-18 11:37:20 +08:00
{
// Add current option to the end of out_stream.
2011-10-29 08:57:28 +08:00
if ( opt_defs [ i ] . required = = false & &
opt_defs [ i ] . option_has_arg = = no_argument )
2010-09-18 11:37:20 +08:00
{
2011-10-29 08:57:28 +08:00
options . insert ( opt_defs [ i ] . short_option ) ;
2010-09-18 11:37:20 +08:00
}
}
}
if ( options . empty ( ) = = false )
{
// We have some required options with no arguments
strm . PutCString ( " [- " ) ;
for ( i = 0 ; i < 2 ; + + i )
for ( options_pos = options . begin ( ) , options_end = options . end ( ) ;
options_pos ! = options_end ;
+ + options_pos )
{
2012-12-04 08:32:51 +08:00
if ( i = = 0 & & : : islower ( * options_pos ) )
2010-09-18 11:37:20 +08:00
continue ;
2012-12-04 08:32:51 +08:00
if ( i = = 1 & & : : isupper ( * options_pos ) )
2010-09-18 11:37:20 +08:00
continue ;
2012-12-04 08:32:51 +08:00
strm < < ( char ) * options_pos ;
2010-09-18 11:37:20 +08:00
}
strm . PutChar ( ' ] ' ) ;
}
2010-09-10 00:44:14 +08:00
// First go through and print the required options (list them up front).
2010-06-16 02:47:14 +08:00
for ( i = 0 ; i < num_options ; + + i )
2010-06-09 00:52:24 +08:00
{
2012-12-06 04:24:57 +08:00
if ( opt_defs [ i ] . usage_mask & opt_set_mask & & isprint8 ( opt_defs [ i ] . short_option ) )
2010-06-09 00:52:24 +08:00
{
2012-12-04 08:32:51 +08:00
if ( opt_defs [ i ] . required & & opt_defs [ i ] . option_has_arg ! = no_argument )
PrintOption ( opt_defs [ i ] , eDisplayBestOption , " " , NULL , true , strm ) ;
2010-09-10 00:44:14 +08:00
}
}
// Now go through again, and this time only print the optional options.
for ( i = 0 ; i < num_options ; + + i )
{
2011-10-29 08:57:28 +08:00
if ( opt_defs [ i ] . usage_mask & opt_set_mask )
2010-09-10 00:44:14 +08:00
{
// Add current option to the end of out_stream.
2012-12-04 08:32:51 +08:00
if ( ! opt_defs [ i ] . required & & opt_defs [ i ] . option_has_arg ! = no_argument )
PrintOption ( opt_defs [ i ] , eDisplayBestOption , " " , NULL , true , strm ) ;
2010-06-09 00:52:24 +08:00
}
}
2012-01-05 03:11:25 +08:00
2012-02-08 09:13:31 +08:00
if ( args_str . GetSize ( ) > 0 )
2012-01-05 03:11:25 +08:00
{
if ( cmd - > WantsRawCommandString ( ) )
strm . Printf ( " -- " ) ;
2012-02-08 09:13:31 +08:00
strm . Printf ( " %s " , args_str . GetData ( ) ) ;
2012-01-05 03:11:25 +08:00
}
2010-06-09 00:52:24 +08:00
}
2012-01-05 03:11:25 +08:00
2012-10-13 01:34:26 +08:00
if ( cmd & &
cmd - > WantsRawCommandString ( ) & &
2012-01-05 03:11:25 +08:00
arguments_str . GetSize ( ) > 0 )
{
strm . PutChar ( ' \n ' ) ;
strm . Indent ( name ) ;
strm . Printf ( " %s " , arguments_str . GetData ( ) ) ;
}
2010-06-09 00:52:24 +08:00
strm . Printf ( " \n \n " ) ;
// Now print out all the detailed information about the various options: long form, short form and help text:
2010-09-18 11:37:20 +08:00
// --long_name <argument> ( -short <argument> )
2010-06-09 00:52:24 +08:00
// help text
// This variable is used to keep track of which options' info we've printed out, because some options can be in
// more than one usage level, but we only want to print the long form of its information once.
2012-12-04 08:32:51 +08:00
std : : multimap < int , uint32_t > options_seen ;
2010-06-09 00:52:24 +08:00
strm . IndentMore ( 5 ) ;
2010-09-10 00:44:14 +08:00
// Put the unique command options in a vector & sort it, so we can output them alphabetically (by short_option)
// when writing out detailed help for each option.
2010-06-09 00:52:24 +08:00
for ( i = 0 ; i < num_options ; + + i )
2012-12-04 08:32:51 +08:00
options_seen . insert ( std : : make_pair ( opt_defs [ i ] . short_option , i ) ) ;
2010-09-10 00:44:14 +08:00
// Go through the unique'd and alphabetically sorted vector of options, find the table entry for each option
// and write out the detailed help information for that option.
2012-12-04 08:32:51 +08:00
bool first_option_printed = false ; ;
for ( auto pos : options_seen )
2010-09-10 00:44:14 +08:00
{
2012-12-04 08:32:51 +08:00
i = pos . second ;
//Print out the help information for this option.
// Put a newline separation between arguments
if ( first_option_printed )
strm . EOL ( ) ;
else
first_option_printed = true ;
CommandArgumentType arg_type = opt_defs [ i ] . argument_type ;
StreamString arg_name_str ;
arg_name_str . Printf ( " <%s> " , CommandObject : : GetArgumentName ( arg_type ) ) ;
strm . Indent ( ) ;
2012-12-06 04:24:57 +08:00
if ( opt_defs [ i ] . short_option & & isprint8 ( opt_defs [ i ] . short_option ) )
2010-09-10 00:44:14 +08:00
{
2012-12-04 08:32:51 +08:00
PrintOption ( opt_defs [ i ] , eDisplayShortOption , NULL , NULL , false , strm ) ;
PrintOption ( opt_defs [ i ] , eDisplayLongOption , " ( " , " ) " , false , strm ) ;
}
else
{
// Short option is not printable, just print long option
PrintOption ( opt_defs [ i ] , eDisplayLongOption , NULL , NULL , false , strm ) ;
}
strm . EOL ( ) ;
strm . IndentMore ( 5 ) ;
if ( opt_defs [ i ] . usage_text )
OutputFormattedUsageText ( strm ,
opt_defs [ i ] . usage_text ,
screen_width ) ;
if ( opt_defs [ i ] . enum_values ! = NULL )
{
strm . Indent ( ) ;
strm . Printf ( " Values: " ) ;
for ( int k = 0 ; opt_defs [ i ] . enum_values [ k ] . string_value ! = NULL ; k + + )
2010-09-10 00:44:14 +08:00
{
2012-12-04 08:32:51 +08:00
if ( k = = 0 )
strm . Printf ( " %s " , opt_defs [ i ] . enum_values [ k ] . string_value ) ;
2010-09-10 00:44:14 +08:00
else
2012-12-04 08:32:51 +08:00
strm . Printf ( " | %s " , opt_defs [ i ] . enum_values [ k ] . string_value ) ;
2010-06-09 00:52:24 +08:00
}
2012-12-04 08:32:51 +08:00
strm . EOL ( ) ;
2010-06-09 00:52:24 +08:00
}
2012-12-04 08:32:51 +08:00
strm . IndentLess ( 5 ) ;
2010-06-09 00:52:24 +08:00
}
// Restore the indent level
strm . SetIndentLevel ( save_indent_level ) ;
}
// This function is called when we have been given a potentially incomplete set of
// options, such as when an alias has been defined (more options might be added at
// at the time the alias is invoked). We need to verify that the options in the set
// m_seen_options are all part of a set that may be used together, but m_seen_options
// may be missing some of the "required" options.
bool
Options : : VerifyPartialOptions ( CommandReturnObject & result )
{
bool options_are_valid = false ;
2010-06-25 04:30:15 +08:00
int num_levels = GetRequiredOptions ( ) . size ( ) ;
2010-06-09 00:52:24 +08:00
if ( num_levels )
{
for ( int i = 0 ; i < num_levels & & ! options_are_valid ; + + i )
{
// In this case we are treating all options as optional rather than required.
// Therefore a set of options is correct if m_seen_options is a subset of the
// union of m_required_options and m_optional_options.
OptionSet union_set ;
2010-06-25 04:30:15 +08:00
OptionsSetUnion ( GetRequiredOptions ( ) [ i ] , GetOptionalOptions ( ) [ i ] , union_set ) ;
2010-06-09 00:52:24 +08:00
if ( IsASubset ( m_seen_options , union_set ) )
options_are_valid = true ;
}
}
return options_are_valid ;
}
bool
Options : : HandleOptionCompletion
(
Args & input ,
OptionElementVector & opt_element_vector ,
int cursor_index ,
int char_pos ,
int match_start_point ,
int max_return_elements ,
2010-06-30 13:02:46 +08:00
bool & word_complete ,
2010-06-09 00:52:24 +08:00
lldb_private : : StringList & matches
)
{
2010-06-30 13:02:46 +08:00
word_complete = true ;
2010-06-09 00:52:24 +08:00
// For now we just scan the completions to see if the cursor position is in
// an option or its argument. Otherwise we'll call HandleArgumentCompletion.
// In the future we can use completion to validate options as well if we want.
const OptionDefinition * opt_defs = GetDefinitions ( ) ;
std : : string cur_opt_std_str ( input . GetArgumentAtIndex ( cursor_index ) ) ;
cur_opt_std_str . erase ( char_pos ) ;
const char * cur_opt_str = cur_opt_std_str . c_str ( ) ;
for ( int i = 0 ; i < opt_element_vector . size ( ) ; i + + )
{
int opt_pos = opt_element_vector [ i ] . opt_pos ;
int opt_arg_pos = opt_element_vector [ i ] . opt_arg_pos ;
int opt_defs_index = opt_element_vector [ i ] . opt_defs_index ;
if ( opt_pos = = cursor_index )
{
// We're completing the option itself.
2010-06-25 04:30:15 +08:00
if ( opt_defs_index = = OptionArgElement : : eBareDash )
{
// We're completing a bare dash. That means all options are open.
// FIXME: We should scan the other options provided and only complete options
// within the option group they belong to.
char opt_str [ 3 ] = { ' - ' , ' a ' , ' \0 ' } ;
2010-07-14 08:18:15 +08:00
for ( int j = 0 ; opt_defs [ j ] . short_option ! = 0 ; j + + )
2010-06-25 04:30:15 +08:00
{
2010-07-14 08:18:15 +08:00
opt_str [ 1 ] = opt_defs [ j ] . short_option ;
2010-06-25 04:30:15 +08:00
matches . AppendString ( opt_str ) ;
}
return true ;
}
else if ( opt_defs_index = = OptionArgElement : : eBareDoubleDash )
{
std : : string full_name ( " -- " ) ;
2010-07-14 08:18:15 +08:00
for ( int j = 0 ; opt_defs [ j ] . short_option ! = 0 ; j + + )
2010-06-25 04:30:15 +08:00
{
full_name . erase ( full_name . begin ( ) + 2 , full_name . end ( ) ) ;
2010-07-14 08:18:15 +08:00
full_name . append ( opt_defs [ j ] . long_option ) ;
2010-06-25 04:30:15 +08:00
matches . AppendString ( full_name . c_str ( ) ) ;
}
return true ;
}
else if ( opt_defs_index ! = OptionArgElement : : eUnrecognizedArg )
2010-06-09 00:52:24 +08:00
{
2013-04-05 04:35:24 +08:00
// We recognized it, if it an incomplete long option, complete it anyway (getopt_long_only is
2010-06-09 00:52:24 +08:00
// happy with shortest unique string, but it's still a nice thing to do.) Otherwise return
// The string so the upper level code will know this is a full match and add the " ".
if ( cur_opt_str & & strlen ( cur_opt_str ) > 2
& & cur_opt_str [ 0 ] = = ' - ' & & cur_opt_str [ 1 ] = = ' - '
& & strcmp ( opt_defs [ opt_defs_index ] . long_option , cur_opt_str ) ! = 0 )
{
std : : string full_name ( " -- " ) ;
full_name . append ( opt_defs [ opt_defs_index ] . long_option ) ;
matches . AppendString ( full_name . c_str ( ) ) ;
return true ;
}
else
{
matches . AppendString ( input . GetArgumentAtIndex ( cursor_index ) ) ;
return true ;
}
}
else
{
// FIXME - not handling wrong options yet:
// Check to see if they are writing a long option & complete it.
// I think we will only get in here if the long option table has two elements
2013-04-05 04:35:24 +08:00
// that are not unique up to this point. getopt_long_only does shortest unique match
2010-06-09 00:52:24 +08:00
// for long options already.
if ( cur_opt_str & & strlen ( cur_opt_str ) > 2
& & cur_opt_str [ 0 ] = = ' - ' & & cur_opt_str [ 1 ] = = ' - ' )
{
2010-07-14 08:18:15 +08:00
for ( int j = 0 ; opt_defs [ j ] . short_option ! = 0 ; j + + )
2010-06-09 00:52:24 +08:00
{
2010-07-14 08:18:15 +08:00
if ( strstr ( opt_defs [ j ] . long_option , cur_opt_str + 2 ) = = opt_defs [ j ] . long_option )
2010-06-09 00:52:24 +08:00
{
std : : string full_name ( " -- " ) ;
2010-07-14 08:18:15 +08:00
full_name . append ( opt_defs [ j ] . long_option ) ;
2010-06-09 00:52:24 +08:00
// The options definitions table has duplicates because of the
// way the grouping information is stored, so only add once.
bool duplicate = false ;
2010-07-14 08:18:15 +08:00
for ( int k = 0 ; k < matches . GetSize ( ) ; k + + )
2010-06-09 00:52:24 +08:00
{
2010-07-14 08:18:15 +08:00
if ( matches . GetStringAtIndex ( k ) = = full_name )
2010-06-09 00:52:24 +08:00
{
duplicate = true ;
break ;
}
}
if ( ! duplicate )
matches . AppendString ( full_name . c_str ( ) ) ;
}
}
}
return true ;
}
}
else if ( opt_arg_pos = = cursor_index )
{
// Okay the cursor is on the completion of an argument.
// See if it has a completion, otherwise return no matches.
if ( opt_defs_index ! = - 1 )
{
2011-04-08 06:46:35 +08:00
HandleOptionArgumentCompletion ( input ,
2010-06-23 09:19:29 +08:00
cursor_index ,
strlen ( input . GetArgumentAtIndex ( cursor_index ) ) ,
opt_element_vector ,
i ,
match_start_point ,
max_return_elements ,
2010-06-30 13:02:46 +08:00
word_complete ,
2010-06-23 09:19:29 +08:00
matches ) ;
2010-06-09 00:52:24 +08:00
return true ;
}
else
{
// No completion callback means no completions...
return true ;
}
}
else
{
// Not the last element, keep going.
continue ;
}
}
return false ;
}
bool
Options : : HandleOptionArgumentCompletion
(
Args & input ,
int cursor_index ,
int char_pos ,
OptionElementVector & opt_element_vector ,
int opt_element_index ,
int match_start_point ,
int max_return_elements ,
2010-06-30 13:02:46 +08:00
bool & word_complete ,
2010-06-09 00:52:24 +08:00
lldb_private : : StringList & matches
)
{
const OptionDefinition * opt_defs = GetDefinitions ( ) ;
2013-04-19 06:45:39 +08:00
std : : unique_ptr < SearchFilter > filter_ap ;
2010-06-09 00:52:24 +08:00
int opt_arg_pos = opt_element_vector [ opt_element_index ] . opt_arg_pos ;
int opt_defs_index = opt_element_vector [ opt_element_index ] . opt_defs_index ;
// See if this is an enumeration type option, and if so complete it here:
OptionEnumValueElement * enum_values = opt_defs [ opt_defs_index ] . enum_values ;
if ( enum_values ! = NULL )
{
bool return_value = false ;
std : : string match_string ( input . GetArgumentAtIndex ( opt_arg_pos ) , input . GetArgumentAtIndex ( opt_arg_pos ) + char_pos ) ;
for ( int i = 0 ; enum_values [ i ] . string_value ! = NULL ; i + + )
{
if ( strstr ( enum_values [ i ] . string_value , match_string . c_str ( ) ) = = enum_values [ i ] . string_value )
{
matches . AppendString ( enum_values [ i ] . string_value ) ;
return_value = true ;
}
}
return return_value ;
}
// If this is a source file or symbol type completion, and there is a
// -shlib option somewhere in the supplied arguments, then make a search filter
// for that shared library.
// FIXME: Do we want to also have an "OptionType" so we don't have to match string names?
2011-04-14 06:47:15 +08:00
uint32_t completion_mask = opt_defs [ opt_defs_index ] . completion_type ;
if ( completion_mask = = 0 )
{
lldb : : CommandArgumentType option_arg_type = opt_defs [ opt_defs_index ] . argument_type ;
if ( option_arg_type ! = eArgTypeNone )
{
CommandObject : : ArgumentTableEntry * arg_entry = CommandObject : : FindArgumentDataByType ( opt_defs [ opt_defs_index ] . argument_type ) ;
if ( arg_entry )
completion_mask = arg_entry - > completion_type ;
}
}
2010-06-09 00:52:24 +08:00
if ( completion_mask & CommandCompletions : : eSourceFileCompletion
| | completion_mask & CommandCompletions : : eSymbolCompletion )
{
for ( int i = 0 ; i < opt_element_vector . size ( ) ; i + + )
{
int cur_defs_index = opt_element_vector [ i ] . opt_defs_index ;
int cur_arg_pos = opt_element_vector [ i ] . opt_arg_pos ;
const char * cur_opt_name = opt_defs [ cur_defs_index ] . long_option ;
// If this is the "shlib" option and there was an argument provided,
// restrict it to that shared library.
if ( strcmp ( cur_opt_name , " shlib " ) = = 0 & & cur_arg_pos ! = - 1 )
{
const char * module_name = input . GetArgumentAtIndex ( cur_arg_pos ) ;
if ( module_name )
{
2010-10-21 04:54:39 +08:00
FileSpec module_spec ( module_name , false ) ;
2011-04-08 06:46:35 +08:00
lldb : : TargetSP target_sp = m_interpreter . GetDebugger ( ) . GetSelectedTarget ( ) ;
2010-06-09 00:52:24 +08:00
// Search filters require a target...
2011-09-17 16:33:22 +08:00
if ( target_sp )
2010-06-09 00:52:24 +08:00
filter_ap . reset ( new SearchFilterByModule ( target_sp , module_spec ) ) ;
}
break ;
}
}
}
2011-04-08 06:46:35 +08:00
return CommandCompletions : : InvokeCommonCompletionCallbacks ( m_interpreter ,
2010-06-23 09:19:29 +08:00
completion_mask ,
input . GetArgumentAtIndex ( opt_arg_pos ) ,
match_start_point ,
max_return_elements ,
filter_ap . get ( ) ,
2010-06-30 13:02:46 +08:00
word_complete ,
2010-06-23 09:19:29 +08:00
matches ) ;
2010-06-09 00:52:24 +08:00
}
2011-04-13 08:18:08 +08:00
2011-04-28 06:04:39 +08:00
void
OptionGroupOptions : : Append ( OptionGroup * group )
{
const OptionDefinition * group_option_defs = group - > GetDefinitions ( ) ;
const uint32_t group_option_count = group - > GetNumDefinitions ( ) ;
for ( uint32_t i = 0 ; i < group_option_count ; + + i )
{
m_option_infos . push_back ( OptionInfo ( group , i ) ) ;
m_option_defs . push_back ( group_option_defs [ i ] ) ;
}
}
2011-04-13 08:18:08 +08:00
void
2011-04-14 06:47:15 +08:00
OptionGroupOptions : : Append ( OptionGroup * group ,
uint32_t src_mask ,
uint32_t dst_mask )
2011-04-13 08:18:08 +08:00
{
const OptionDefinition * group_option_defs = group - > GetDefinitions ( ) ;
const uint32_t group_option_count = group - > GetNumDefinitions ( ) ;
for ( uint32_t i = 0 ; i < group_option_count ; + + i )
{
2011-04-14 06:47:15 +08:00
if ( group_option_defs [ i ] . usage_mask & src_mask )
{
m_option_infos . push_back ( OptionInfo ( group , i ) ) ;
m_option_defs . push_back ( group_option_defs [ i ] ) ;
m_option_defs . back ( ) . usage_mask = dst_mask ;
}
2011-04-13 08:18:08 +08:00
}
}
void
OptionGroupOptions : : Finalize ( )
{
m_did_finalize = true ;
OptionDefinition empty_option_def = { 0 , false , NULL , 0 , 0 , NULL , 0 , eArgTypeNone , NULL } ;
m_option_defs . push_back ( empty_option_def ) ;
}
Error
OptionGroupOptions : : SetOptionValue ( uint32_t option_idx ,
const char * option_value )
{
// After calling OptionGroupOptions::Append(...), you must finalize the groups
// by calling OptionGroupOptions::Finlize()
assert ( m_did_finalize ) ;
2011-04-14 06:47:15 +08:00
assert ( m_option_infos . size ( ) + 1 = = m_option_defs . size ( ) ) ;
Error error ;
if ( option_idx < m_option_infos . size ( ) )
2011-04-13 08:18:08 +08:00
{
2011-04-14 06:47:15 +08:00
error = m_option_infos [ option_idx ] . option_group - > SetOptionValue ( m_interpreter ,
m_option_infos [ option_idx ] . option_index ,
option_value ) ;
}
else
{
error . SetErrorString ( " invalid option index " ) ; // Shouldn't happen...
2011-04-13 08:18:08 +08:00
}
return error ;
}
void
OptionGroupOptions : : OptionParsingStarting ( )
{
2011-04-14 06:47:15 +08:00
std : : set < OptionGroup * > group_set ;
OptionInfos : : iterator pos , end = m_option_infos . end ( ) ;
for ( pos = m_option_infos . begin ( ) ; pos ! = end ; + + pos )
{
OptionGroup * group = pos - > option_group ;
if ( group_set . find ( group ) = = group_set . end ( ) )
{
group - > OptionParsingStarting ( m_interpreter ) ;
group_set . insert ( group ) ;
}
}
2011-04-13 08:18:08 +08:00
}
Error
OptionGroupOptions : : OptionParsingFinished ( )
{
2011-04-14 06:47:15 +08:00
std : : set < OptionGroup * > group_set ;
2011-04-13 08:18:08 +08:00
Error error ;
2011-04-14 06:47:15 +08:00
OptionInfos : : iterator pos , end = m_option_infos . end ( ) ;
for ( pos = m_option_infos . begin ( ) ; pos ! = end ; + + pos )
2011-04-13 08:18:08 +08:00
{
2011-04-14 06:47:15 +08:00
OptionGroup * group = pos - > option_group ;
if ( group_set . find ( group ) = = group_set . end ( ) )
{
error = group - > OptionParsingFinished ( m_interpreter ) ;
group_set . insert ( group ) ;
if ( error . Fail ( ) )
return error ;
}
2011-04-13 08:18:08 +08:00
}
return error ;
}