Skip to content

Commands

Matthias Beerens edited this page Sep 25, 2020 · 3 revisions

Commands

A command is an argument that can execute code and/or have its own subset of options when provided.

Using the Fluent API

NOTE: You can add a command to the parser with or without a subset of options.

static int Main(string[] args)
{
   var parser = new CommandLineParser<Options>();

   parser.AddCommand()
        .Name("start")
        .Description("Start the server command.")
        .Required()
        .OnExecuting(baseOptions => Console.WriteLine("Start command executed!"));

   // further configuration..
}

If you want to have there own subset of options you can specify them as well.

static int Main(string[] args)
{
   var parser = new CommandLineParser<Options>();

   parser.AddCommand<SubsetOptions>()
        .Name("start")
        .Description("Start the server command.")
        .Required()
        .OnExecuting((baseOptions, subsetOptions) 
            => Console.WriteLine($"Start command executed with verbose: {subsetOptions.Verbose}!"));

   // further configuration..
}

class SubsetOptions
{
   [Name("v", "verbose"), Required, Description("Sets if the output should be verbose.")]
   public bool Verbose { get; set; }
}

You can also configure the subset options using the fluent api.

static int Main(string[] args)
{
   var parser = new CommandLineParser<Options>();

   var startCmd = parser.AddCommand<SubsetOptions>()
      .Name("start")
      .Description("Start the server command.")
      .Required()
      .OnExecuting((baseOptions, subsetOptions) 
         => Console.WriteLine($"Start command executed with verbose: {subsetOptions.Verbose}!"));

   startCmd.Configure(opt => opt.Verbose)
       .Name("v", "verbose")
       .Description("Sets if the output should be verbose.")
       .Required();

   // further configuration..
}

class SubsetOptions
{
   public bool Verbose { get; set; }
}

Register a custom command

This is a more advanced way of adding commands but they also allow for more flexability. This way the commands can for example be constructed using a DI/IoC container.

public class StartCommand : Command<Options, SubsetOptions>
{
   public override void OnConfigure(ICommandConfigurationBuilder<SubsetOptions> builder)
   {
       builder
          .Name("start")
          .Required();
   }

   public override void OnExecute(Options options, SubsetOptions commandOptions)
   {
       base.OnExecute(options, commandOptions);

       Console.WriteLine("Start command executed with verbose: {commandOptions.Verbose}");
   }
}

public class SubsetOptions
{
   public bool Verbose { get; set; }
}

static int Main(string[] args)
{
   var parser = new CommandLineParser<Options>();

   parser.RegisterCommand<StartCommand, SubsetOptions>();

   // further configuration..
}

Note: If you want to inject dependencies using the constructor you will need to see Dependency Injection Wiki for more.

Add a command using the model

You don't need to call the RegisterCommand as you can also add it as a property to the options model class.

public class StartCommand : Command<Options, SubsetOptions>
{
   public override void OnConfigure(ICommandConfigurationBuilder<SubsetOptions> builder)
   {
       builder
          .Name("start")
          .Required();
   }

   public override void OnExecute(Options options, SubsetOptions commandOptions)
   {
       base.OnExecute(options, commandOptions);

       Console.WriteLine("Start command executed with verbose: {commandOptions.Verbose}");
   }
}

public class SubsetOptions
{
   public bool Verbose { get; set; }
}

public class Options
{
   // The command will be automatically configured because it is defined in the model
   public StartCommand StartCommand { get; set; }
}

static int Main(string[] args)
{
   var parser = new CommandLineParser<Options>();

   // further configuration..
}

Note: This way you can create a tree structure of commands with subcommands.

Discover commands

This will load all of the commands from the given assemblies that inherit from MatthiWare.CommandLine.Abstractions.Command.Command class.

static int Main(string[] args)
{
   var parser = new CommandLineParser<Options>();

   parser.DiscoverCommands(Assembly.GetExecutingAssembly());
   
   // further configuration..
}