‹‹ CommandLine Console QuickStart using the CommandLine library

Here's the basic structure of your Program.cs file...

class Program
{
    //The main command line object.
    private static CommandLineType _cl;

    static void Main(string[] args) {...}
    private static bool Parm_ValidateFunction(object sender) {...}
    private static bool TestCommand_CommandFunction(object sender) {...}
    private static void DisplayWelcome(CommandLineType cl) {...}
}

This Main() method creates 1 command for "TEST" that accepts up to 2 parameters (SomeParm & SomethingElse), adds the parms to the command, adds the command to the command line, and tells the command line to start accepting commands. You can add as many commands and parameters as you need.

static void Main(string[] args)
{
    _cl = new CommandLineType("Example", ">");

    var testCommand = new CommandType(TestCommand_CommandFunction, "test", "Just a sample command.");
    testCommand.ParameterList.Add(new ParameterType(Parm_ValidateFunction, "SomeParm", "=", "Hello", "A sample command parameter that defaults to value \"Hello\"."));
    testCommand.ParameterList.Add(new ParameterType(Parm_ValidateFunction, "SomethingElse", "=", "World", "A sample command parameter that defaults to value \"World\"."));

    _cl.AddCommand(testCommand);
    _cl.SetExitCommand(new CommandType((NotifyEventDelegate)null, "Quit", "My custom exit command."));

    if (args.Length > 0)
        _cl.ExecDosCommand(args);
    else
    {
        DisplayWelcome(_cl);
        _cl.Execute();
    }
}

This is automatically called when the user enters the TEST command AND provides parameters. This method validates the values the user provided and returns true or false if they're good or bad. If bad, processing of the command stops here. In this example, I'm treating the parameter value of "fail" as bad.

private static bool Parm_ValidateFunction(object sender)
{
    var parm = (ParameterType) sender;
    _cl.OutputStream.WriteLine($"Parameter recognized \"{parm.ParamText}\" with value \"{parm.UserValue}\".");

    if (parm.UserValue.ToLower() == "fail")
        return false;

    return true;
}

This is automatically called if the user enters the TEST command AND the parameters they enter have been validated. It won't make it here unless everything is good.

private static bool TestCommand_CommandFunction(object sender)
{
    var command = (CommandType) sender;

    var cl = command.GetOwnerCommandLine();
    cl.OutputStream.WriteLine($"You entered [{command.ConsoleEntry}]");

    if (command.ConsoleEntry.Contains("fail"))
        return false;

    return true;
}

Below is what the console app looks like when run. Notice the bult-in Help facility and the Quit command? If you don't provide a quit command, you'll get an EXIT command for free.



That's it! Just create your one CommandLine object, add commands to it, add your parameters to your commands, wire up your event handlers, then tell your command line object to execute. Done! Everything else is handled for you, plus you get command history for free, help for free, exit command for free, parsing for free, routing for free.

I'm only providing this DisplayWelcome() method as an example of what you might want to add. This has NOTHING to do with the CommandLine library. But, notice how it's outputing to the command line's OutputStream and NOT to Console Out? That's IMPORTANT! Make sure you use that!

private static void DisplayWelcome(CommandLineType cl)
{
    cl.OutputStream.WriteLine("*******************************************************************");
    cl.OutputStream.WriteLine("*                                                                 *");
    cl.OutputStream.WriteLine("*  #     #           ##                                       ##  *");
    cl.OutputStream.WriteLine("*  #     #            #                                       ##  *");
    cl.OutputStream.WriteLine("*  #     #            #                                       ##  *");
    cl.OutputStream.WriteLine("*  #     #            #                                       ##  *");
    cl.OutputStream.WriteLine("*  #  #  #  #####     #     #####   #####  ### ##   #####     ##  *");
    cl.OutputStream.WriteLine("*  # ### # #     #    #    #     # #     # ## #  # #     #    ##  *");
    cl.OutputStream.WriteLine("*  ### ### #######    #    #       #     # #  #  # #######    ##  *");
    cl.OutputStream.WriteLine("*  ##   ## #          #    #     # #     # #  #  # #              *");
    cl.OutputStream.WriteLine("*  #     #  #####    ###    #####   #####  #     #  #####     ##  *");
    cl.OutputStream.WriteLine("*                                                                 *");
    cl.OutputStream.WriteLine("*******************************************************************\r\n");

    cl.OutputStream.WriteLine("Welcome to the CommandLine sample Console application.");
    cl.OutputStream.WriteLine("Type \"Help\".");
}
‹‹ CommandLine