Hello, World!

We'll use the classic Hello World example to show how to create custom commands for Xshell.

The Anatomy of a Custom Command

A custom command is implemented by creating a class that implements the IXsCommand interface. If the command should accept input or configuration from the TaskSet, like the path to a file, a separate class implementing the IXsCommandArgs interface will need to be created. Our Hello World command will allow the user to specify their name in the configuration.

Create the Project

To get started, create a new DLL Library in your language of choice - C# or VB.NET. Next, you'll need a reference to the Xshell.Base library, so go ahead and add it. The attached solution contains a build of Xshell.Base from r51403.

Create the Command

Create a new class called HelloWorldCommand and implement the Xshell.IXsCommand interface. Your class should look like this:

using System;

using Xshell;

namespace HelloWorldXsc
{
    public class HelloWorldCommand : IXsCommand
    {
        #region IXsCommand Members

        public void ExecuteCommand(IXsCommandArgs args)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

Create the Command Argument Container

Now we need to create a class that will hold the data for our command. Create a new class called HelloWorldCommandArgs and implement the IXsCommandArgs interface. Go ahead and add two public string properties, FirstName and LastName, to the class.

using System;

namespace HelloWorldXsc
{
    public class HelloWorldCommandArgs : Xshell.IXsCommandArgs
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

Decoration is Key

The Xshell engine translates <HelloWorld> to HelloWorldCommand using attributes. Decorate the HelloWorldCommand class like so:

[XsCommand(Name = "HelloWorld", CommandArgsContainerType = typeof(HelloWorldCommandArgs))]
public class HelloWorldCommand : IXsCommand
{
    ... // removed irrelevant code
}

Make it do something

Go back to the HelloWorldCommand class and give the ExecuteCommand method some life:

public void ExecuteCommand(IXsCommandArgs args)
{
    HelloWorldCommandArgs helloArgs = (HelloWorldCommandArgs)args;
    Console.WriteLine("Hello there, {0} {1}!", helloArgs.FirstName, helloArgs.LastName);
    Console.Read();
}

Setting up the Xshell Engine

Coming soon, but it's easy: just XML

Last edited May 26, 2010 at 6:22 PM by wgraham, version 1

Comments

No comments yet.