Processing a Command Method

A command method is processed by accessing the CommandLine instance passed to the method when called by the framework.

The CommandLine instance contains information about the text entered by the user including the command name, the argument string, an array of the words parsed from the string, and the original text entered by the user (with and without any command token, as applicable). The CommandLine also offers a ParseArguments() method which can assist in seperating argument values which may contain multi-word strings wrapped in quotes, and a LogEvent() method which allows you to associate a log event with the processing of this line.

While processing advanced commands, you may wish to capture additional information from the user during execution of the command. The ConsoleInput class is availble for this purpose and offers a number of Get methods to retrieve qualified input from the user. The ConsoleInput class also provides support for the display of simple menus. See Capturing Input for more details.

When your code is finished processing the command, you have the option of supplying a final result message via the SetMessage() method on the CommandLine. Some complex commands with multiline output may have provided all of the output display during processing via the ConsoleOutput methods. In this case calling SetMessage() may not be required. For more information on advanced output, see Displaying Output.

However, if the command fails in some way, you should call SetMessage(errorstring, True) to return an error result to the user and automatically log the error in the framework event log.

Custom Command Processing Using Events

You can also handle the CommandInterpreter.ProcessCommandLine event to provide custom processing of a console command. You can test the user input and modify it before passing it onto the default command processor, or you can provide your own functionality to execute the command.

Event-Based Processing Example

Imports CommandConsoleFramework

Module Module1
    Sub Main()
        AddHandler CommandInterpreter.ProcessCommandLine, AddressOf CommandInterpreter_ProcessCommandLine
        CommandInterpreter.Run()
    End Sub

    Private Sub CommandInterpreter_ProcessCommandLine(send As Object, e As ProcessCommandLineEventArgs)
        If e.Line.CommandName = "?" Then
            e.Line.CommandName = "help"
            e.HandledByCommandMethod = True
        ElseIf e.Line.CommandName = "test" Then
            e.Line.SetResultMessage("1...2...3...")
            e.HandledByCustom = True
        End If
    End Sub
End Module

Last edited May 18, 2012 at 4:10 AM by ReedKimble, version 2

Comments

No comments yet.