Ploch CommandLine Applications provides useful extensions to the CommandLineUtils library.
Features:
- Preconfigured Dependency Injection
- Preconfigured Microsoft Extensions Configuration
- Simple, single-line, building of a configured and executable app
- Interfaces for building commands
- Simplified configuration for verbs (commands), including nested verbs
- Streamlined configuration for
- Autofac
- Serilog
-
Create a console application project
-
Create a command class which includes executable code and commandline options:
using McMaster.Extensions.CommandLineUtils;
using Ploch.Common.CommandLine;
[Command(Name = "command1")]
public class RootCommand1(ISomeInterface dependency)
{
[Option]
public bool Verbose { get; set; }
[Option]
public string? Colour { get; set; }
// Inferred type = MultipleValues
// Defined names = "-N"
public void OnExecute()
{
Console.WriteLine($"Command1 executed");
}
}- Update the
Program.csfile:
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Ploch.Common.CommandLine;
using Ploch.Common.CommandLine.Autofac;
// Demonstrates verbs with verbs using commands
await AppBuilder.CreateDefault(cfg => cfg.AddJsonFile("appsettings.json"))
.Configure(container =>
{
// Add services
container.ServiceCollection.AddSingleton<RootCommand1>()
.AddSingleton<ISomeInterface, SomeClass>();
container.Application.Command<RootCommand1>();
})
.Build()
.ExecuteAsync(args);- Run:
myapp.exe --help
myapp.exe command1 --verbose --colour Pink