The Commander

The main class that does all

The Commander is the main object within ClArgs. As you have seen in the introduction with a single line of code Commander.ExecuteCommand( args) Commander will do:

  • parse the command-line

  • resolve existing Commands

  • transform command-line arguments (strings) into typed Options

  • create a typed Parameters object

  • run the appropriate Command (based on the provided Verb(s))

  • do the error-handling

  • display help messages if requested

The simplest way to use the Commander is by using its static ExecuteCommand() method. Alternatively, you may create your own instance new Commander(...).

public static void ExecuteCommand(string[] args, Settings settings = null) =>
    new Commander(settings).ExecuteCommand(CommandLineParser.Parse(args));

Annotations

The Commander normally relies on Annotations, like[Command], [OptionDescriptor] or [OtionSet].

[Command("HelloWorld", "This is the help text of my HelloWorld command.")]
internal class HelloWorldCommand : CommandBase<HelloWorldParameters>
{ }

class HelloWorldParameters
{ 
  [OptionDescriptor("country", "c", Required = true, 
    HelpText = "The country you're sending greetings to.")]
  public string Country { get; set; }

Command Annotation

Classes that implement a Command must have this annotation.

CommandAttribute(string verb, string helpText = null)

Verb - is mandatory and it is used to find the right Command.

HelpText - is optional and it will be displayed whenever help is requested

Last updated