Command Parameters
Commands that you create for your add-in can accept parameters. You can pass these parameters to the command handler when they're invoked from the Command Window or by using the DTE.ExecuteCommand method. For example, suppose your add-in created a command with the name MyAddin.Connect.Command. If the user types into the Command Window the lines
MyAddin.Connect.Command
MyAddin.Connect.Command My Parameters
the IDTCommandTarget.Exec method of your add-in is called twice. The first time, the VarIn parameter is an empty string ("). The second time, VarIn contains the string "My Parameters". Everything after the name of the command (except trailing and ending white space) is copied verbatim into the VarIn parameter. It's up to your add-in to parse the list of supplied parameters and handle them as needed. Parameters can also be passed using the ExecuteCommand method; up to this point, we've only seen ExecuteCommand being passed one argumentthe full name of the command. However, you can pass an optional second argument, which is the parameter that's passed to your command handler the same way that data is passed when the command is invoked through the Command Window.Commands that support parameters often require a specific syntax for what can be passed in. A user might be able to look up in the documentation exactly what that syntax is, but it would be easier for the user to ask the command directly for help. A common format for finding help from command-line utilities in Windows is to type the name of the program followed by a /? argument. The Visual Studio .NET Command Window also supports help in a similar fashion. If the user types the command name followed by /? into the Command Window, your IDTCommandTarget.Exec method is called with the ExecuteOption parameter set to a value OR'ed together with vsCommandExecOption.vsCommandExecOptionShowHelp. When this value is passed, your add-in can decide the best method of displaying help, such as displaying a Web page with documentation. The CommandHelp add-in included with the book's sample files demonstrates displaying a Help file when the /? parameter is specified to a command.Macros can also take parameters. To declare a macro that takes arguments, you must make all the arguments optional strings with a defined default value, as demonstrated in this macro:
Sub OptionalArguments(Optional ByVal optArg As String = "I am optional")
MsgBox(optArg)
End Sub
This macro can be called from the Command Window or the ExecuteCommand method, using either just its name or the name and arguments. If the argument isn't specified when this macro is called, a message box with the text "I am optional" is displayed to the user. If an argument is specified, the message box uses that argument text. Macros don't provide special functionality for invoking help, but you can simulate this functionality by checking the argument value passedif it's set to the string /?, the user has requested help. You can rewrite the macro as follows to handle help:
Sub OptionalArguments2(Optional ByVal optArg As String = "I am optional")
If (optArg = "/?") Then
Dim helpString As String
helpString = "Usage: OptionalArguments [optArg]"
helpString = helpString + vbLf
helpString = helpString + "This macro will display in " + _
"a message box the passed argument."
MsgBox(helpString)
Else
MsgBox(optArg)
End If
End Sub