Added two new classes for command options:

    lldb_private::OptionGroup
    lldb_private::OptionGroupOptions

OptionGroup lets you define a class that encapsulates settings that you want
to reuse in multiple commands. It contains only the option definitions and the
ability to set the option values, but it doesn't directly interface with the
lldb_private::Options class that is the front end to all of the CommandObject
option parsing. For that the OptionGroupOptions class can be used. It aggregates
one or more OptionGroup objects and directs the option setting to the 
appropriate OptionGroup class. For an example of this, take a look at the 
CommandObjectFile and how it uses its "m_option_group" object shown below
to be able to set values in both the FileOptionGroup and PlatformOptionGroup
classes. The members used in CommandObjectFile are:

    OptionGroupOptions m_option_group;
    FileOptionGroup m_file_options;
    PlatformOptionGroup m_platform_options;

Then in the constructor for CommandObjectFile you can combine the option
settings. The code below shows a simplified version of the constructor:

CommandObjectFile::CommandObjectFile(CommandInterpreter &interpreter) :
    CommandObject (...),
    m_option_group (interpreter),
    m_file_options (),
    m_platform_options(true)
{
    m_option_group.Append (&m_file_options);
    m_option_group.Append (&m_platform_options);
    m_option_group.Finalize();
}

We append the m_file_options and then the m_platform_options and then tell
the option group the finalize the results. This allows the m_option_group to
become the organizer of our prefs and after option parsing we end up with
valid preference settings in both the m_file_options and m_platform_options
objects. This also allows any other commands to use the FileOptionGroup and
PlatformOptionGroup classes to implement options for their commands.

Renamed:
    virtual void Options::ResetOptionValues();
to:
    virtual void Options::OptionParsingStarting();

And implemented a new callback named:

    virtual Error Options::OptionParsingFinished();
    
This allows Options subclasses to verify that the options all go together
after all of the options have been specified and gives the chance for the
command object to return an error. It also gives a chance to take all of the
option values and produce or initialize objects after all options have
completed parsing.

Modfied:

    virtual Error
    SetOptionValue (int option_idx, const char *option_arg) = 0;
    
to be:

    virtual Error
    SetOptionValue (uint32_t option_idx, const char *option_arg) = 0;

(option_idx is now unsigned).




git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@129415 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Commands/CommandObjectPlatform.cpp b/source/Commands/CommandObjectPlatform.cpp
index 730a1b3..46a53c7 100644
--- a/source/Commands/CommandObjectPlatform.cpp
+++ b/source/Commands/CommandObjectPlatform.cpp
@@ -27,24 +27,134 @@
 using namespace lldb;
 using namespace lldb_private;
 
+
+PlatformSP 
+PlatformOptionGroup::CreatePlatformWithOptions (CommandInterpreter &interpreter,
+                                                const char *platform_name, 
+                                                bool select, 
+                                                Error& error)
+{
+    if (platform_name && platform_name[0])
+    {
+        if (platform_sp)
+        {
+            error.SetErrorString ("platform can't be set more than once in a command");
+            return PlatformSP();
+        }
+
+        platform_sp = Platform::Create (platform_name, error);
+
+        if (platform_sp)
+        {
+                interpreter.GetDebugger().GetPlatformList().Append (platform_sp, select);
+                if (os_version_major != UINT32_MAX)
+                {
+                    platform_sp->SetOSVersion (os_version_major,
+                                               os_version_minor,
+                                               os_version_update);
+                }
+        }
+    }
+    else
+    {
+        error.SetErrorString ("invalid platform name");
+        platform_sp.reset();
+    }
+    return platform_sp;
+}
+
+void
+PlatformOptionGroup::OptionParsingStarting (CommandInterpreter &interpreter)
+{
+    platform_sp.reset();
+    os_version_major = UINT32_MAX;
+    os_version_minor = UINT32_MAX;
+    os_version_update = UINT32_MAX;
+}
+
+static OptionDefinition
+g_option_table[] =
+{
+    { LLDB_OPT_SET_ALL, false, "platform"   , 'p', required_argument, NULL, 0, eArgTypeNone, "Specify name of the platform to use for this target, creating the platform if necessary."},
+    { LLDB_OPT_SET_ALL, false, "sdk-version", 'v', required_argument, NULL, 0, eArgTypeNone, "Specify the initial SDK version to use prior to connecting." }
+};
+
+static const uint32_t k_option_table_size = sizeof(g_option_table)/sizeof (OptionDefinition);
+
+const OptionDefinition*
+PlatformOptionGroup::GetDefinitions ()
+{
+    if (m_include_platform_option)
+        return g_option_table;
+    return g_option_table + 1;
+}
+
+uint32_t
+PlatformOptionGroup::GetNumDefinitions ()
+{
+    if (m_include_platform_option)
+        return k_option_table_size;
+    return k_option_table_size - 1;
+}
+
+
+Error
+PlatformOptionGroup::SetOptionValue (CommandInterpreter &interpreter,
+                                     uint32_t option_idx,
+                                     const char *option_arg)
+{
+    Error error;
+    if (!m_include_platform_option)
+        --option_idx;
+        
+    char short_option = (char) g_option_table[option_idx].short_option;
+    
+    switch (short_option)
+    {
+    case 'p':
+        CreatePlatformWithOptions (interpreter, option_arg, true, error);
+        break;
+
+    case 'v':
+        if (Args::StringToVersion (option_arg, os_version_major, os_version_minor, os_version_update) == option_arg)
+        {
+            error.SetErrorStringWithFormat ("invalid version string '%s'", option_arg);
+        }
+        else
+        {
+            if (platform_sp)
+                platform_sp->SetOSVersion (os_version_major, os_version_minor, os_version_update);
+        }
+        break;
+        
+    default:
+        error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
+        break;
+    }
+    return error;
+}
+
 //----------------------------------------------------------------------
 // "platform create <platform-name>"
 //----------------------------------------------------------------------
-class CommandObjectPlatformCreate : public CommandObject
+class CommandObjectPlatformSelect : public CommandObject
 {
 public:
-    CommandObjectPlatformCreate (CommandInterpreter &interpreter) :
+    CommandObjectPlatformSelect (CommandInterpreter &interpreter) :
         CommandObject (interpreter, 
-                       "platform create",
-                       "Create a platform instance by name and select it as the current platform.",
-                       "platform create <platform-name>",
+                       "platform select",
+                       "Create a platform if needed and select it as the current platform.",
+                       "platform select <platform-name>",
                        0),
-        m_options (interpreter)
+        m_option_group (interpreter),
+        m_platform_options (false) // Don't include the "--platform" option by passing false
     {
+        m_option_group.Append (&m_platform_options);
+        m_option_group.Finalize();
     }
 
     virtual
-    ~CommandObjectPlatformCreate ()
+    ~CommandObjectPlatformSelect ()
     {
     }
 
@@ -54,20 +164,13 @@
         Error error;
         if (args.GetArgumentCount() == 1)
         {
-            PlatformSP platform_sp (Platform::Create (args.GetArgumentAtIndex (0), error));
-            
+            const bool select = true;
+            PlatformSP platform_sp (m_platform_options.CreatePlatformWithOptions (m_interpreter,
+                                                                                  args.GetArgumentAtIndex (0), 
+                                                                                  select, 
+                                                                                  error));
             if (platform_sp)
-            {
-                m_interpreter.GetDebugger().GetPlatformList().Append (platform_sp, true);
-                if (m_options.os_version_major != UINT32_MAX)
-                {
-                    platform_sp->SetOSVersion (m_options.os_version_major,
-                                               m_options.os_version_minor,
-                                               m_options.os_version_update);
-                }
-                
                 platform_sp->GetStatus (result.GetOutputStream());
-            }
         }
         else
         {
@@ -80,86 +183,12 @@
     virtual Options *
     GetOptions ()
     {
-        return &m_options;
+        return &m_option_group;
     }
 
 protected:
-
-    class CommandOptions : public Options
-    {
-    public:
-
-        CommandOptions (CommandInterpreter &interpreter) :
-            Options (interpreter),
-            os_version_major (UINT32_MAX),
-            os_version_minor (UINT32_MAX),
-            os_version_update (UINT32_MAX)
-        {
-        }
-
-        virtual
-        ~CommandOptions ()
-        {
-        }
-
-        virtual Error
-        SetOptionValue (int option_idx, const char *option_arg)
-        {
-            Error error;
-            char short_option = (char) m_getopt_table[option_idx].val;
-
-            switch (short_option)
-            {
-            case 'v':
-                if (Args::StringToVersion (option_arg, 
-                                           os_version_major,
-                                           os_version_minor,
-                                           os_version_update) == option_arg)
-                {
-                    error.SetErrorStringWithFormat ("invalid version string '%s'", option_arg);
-                }
-                break;
-
-            default:
-                error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
-                break;
-            }
-
-            return error;
-        }
-
-        void
-        ResetOptionValues ()
-        {
-            os_version_major = UINT32_MAX;
-            os_version_minor = UINT32_MAX;
-            os_version_update = UINT32_MAX;
-        }
-
-        const OptionDefinition*
-        GetDefinitions ()
-        {
-            return g_option_table;
-        }
-
-        // Options table: Required for subclasses of Options.
-
-        static OptionDefinition g_option_table[];
-
-        // Instance variables to hold the values for command options.
-
-        uint32_t os_version_major;
-        uint32_t os_version_minor;
-        uint32_t os_version_update;
-    };
-    CommandOptions m_options;
-};
-
-OptionDefinition
-CommandObjectPlatformCreate::CommandOptions::g_option_table[] =
-{
-    { LLDB_OPT_SET_ALL, false, "sdk-version", 'v', required_argument, NULL, 0, eArgTypeNone, "Specify the initial SDK version to use prior to connecting." },
-    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
+    OptionGroupOptions m_option_group;
+    PlatformOptionGroup m_platform_options;
 };
 
 //----------------------------------------------------------------------
@@ -256,37 +285,6 @@
     }
 };
 
-
-//----------------------------------------------------------------------
-// "platform select <platform-name>"
-//----------------------------------------------------------------------
-class CommandObjectPlatformSelect : public CommandObject
-{
-public:
-    CommandObjectPlatformSelect (CommandInterpreter &interpreter) :
-        CommandObject (interpreter, 
-                       "platform select",
-                       "Select a platform by name to be the currently selected platform.",
-                       "platform select <platform-name>",
-                       0)
-    {
-    }
-
-    virtual
-    ~CommandObjectPlatformSelect ()
-    {
-    }
-
-    virtual bool
-    Execute (Args& args, CommandReturnObject &result)
-    {
-        result.AppendError ("command not implemented\n");
-        result.SetStatus (eReturnStatusFailed);
-        return result.Succeeded();
-    }
-};
-
-
 //----------------------------------------------------------------------
 // "platform connect <connect-url>"
 //----------------------------------------------------------------------
@@ -683,7 +681,7 @@
         }
         
         virtual Error
-        SetOptionValue (int option_idx, const char *option_arg)
+        SetOptionValue (uint32_t option_idx, const char *option_arg)
         {
             Error error;
             char short_option = (char) m_getopt_table[option_idx].val;
@@ -773,7 +771,7 @@
         }
         
         void
-        ResetOptionValues ()
+        OptionParsingStarting ()
         {
             match_info.Clear();
             show_args = false;
@@ -956,11 +954,10 @@
     CommandObjectMultiword (interpreter,
                             "platform",
                             "A set of commands to manage and create platforms.",
-                            "platform [connect|create|disconnect|info|list|status|select] ...")
+                            "platform [connect|disconnect|info|list|status|select] ...")
 {
-    LoadSubCommand ("create", CommandObjectSP (new CommandObjectPlatformCreate  (interpreter)));
-    LoadSubCommand ("list"  , CommandObjectSP (new CommandObjectPlatformList    (interpreter)));
     LoadSubCommand ("select", CommandObjectSP (new CommandObjectPlatformSelect  (interpreter)));
+    LoadSubCommand ("list"  , CommandObjectSP (new CommandObjectPlatformList    (interpreter)));
     LoadSubCommand ("status", CommandObjectSP (new CommandObjectPlatformStatus  (interpreter)));
     LoadSubCommand ("connect", CommandObjectSP (new CommandObjectPlatformConnect  (interpreter)));
     LoadSubCommand ("disconnect", CommandObjectSP (new CommandObjectPlatformDisconnect  (interpreter)));