Demo6 - Context Sets

In case you want to implement many commands that share the same Context information, you can use inheritance or Context sets.

public class ApiContext : SharedContext
{
    [OptionDescriptor("P1", 
        required:false, defaultValue :"my default", 
        helpText:"Specify something.")]
    public string P1 { get; set; }

    [OptionSet]
    public ConnectionParameters ConnectionParameters{ get; set; }
}

// This information is used by many other Commands, too.
public class ConnectionParameters
{
    [OptionDescriptor("Username", required:true)]
    public string Username { get; set; }

    [OptionDescriptor("Password", required:true)]
    public string Password { get; set; }
}

public class ApiCommand : CommandBase2<ApiContext>
{
  //..
    protected override void Execute()
    {
        Print.Info($"P1      = {this.Context.P1}");
        Print.Info($"Url     = {this.Context.Url}");
        Print.Info($"Username= {this.Context.ConnectionParameters.Username}");
        Print.Info($"Password= {this.Context.ConnectionParameters.Password}");
    }
    //...
> demo6app API /url http://localhost /username markus /password schmidt

P1      = my default
Url     = http://localhost
Username= markus
Password= schmidt

Last updated