Command Parameters

Command-Line options

Command-line Options are turned into Parameters. Each Command has its own set of parameters.

app.exe --LocalDateTime='2020-08-01 08:10:00' --LocalTimeZone='Pacific Standard Time'

[Command("ConvertToUtc")]
class ConvertToUtcCommand : CommandBase<ConvertToUtcParameters>
{}

A good naming convention is, to name the Command class <Verb>Command and the Parameters class <Verb>Parameters as shown in the example above. Anyway, this is not mandatory.

A Verb defines the Command that is executed and Parameters take the Options which were provided in the command-line.

Check out the first example on GitHub 'ConvertToUTC'.

class ConvertToUtcParameters
{
  [OptionDescriptor("LocalDateTime", required:true, 
    helpText:"A local date and time that should be converted into UTC.")]
  public DateTime LocalDateTime { get; set; }

  [OptionDescriptor("LocalTimeZone", required:true, 
    helpText:"Specify the LocalDateTime's time zone")]
  public string LocalTimeZone { get; set; }
}

The Commander automatically creates a new instance of the Parameters class that belongs to the Command which will be executed and passed it to the Command's Execute method.

class ConvertToUtcCommand : CommandBase<ConvertToUtcParameters>
{
  protected override void Execute( ConvertToUtcParameters ps)
  { }
}

Complex Parameter classes

Parameter classes can inherit form base-classes (see BaseParameters) and they can contain complex properties which itself are Parameter classes (see OptionSet: Connection).

class DBConvertParameters : BaseParameters
{
    [OptionSet]
    public Connection DbConnection { get; set; }
    
    [OptionDescriptor("DatabaseTableName", "t", Required = false)]
    public string DatabaseTableName { get; set; }
}

class BaseParameters
{
    [OptionDescriptor("BaseSetting", "bs", Required = true)]
    public bool BaseSetting { get; set; }
}

class Connection
{
    [OptionDescriptor("u", Required = true)]
    public string UserName { get; set; }

    [OptionDescriptor("p", Required = true)]
    public string Password { get; set; }

    [OptionDescriptor("d", Required = false)]
    public string Domain { get; set; }
}

This makes the Parameters extremely powerful and flexible, especially if you want to support multiple Verbs/Commands which may share the same options.

Last updated