Quick Start
Bootstrapping
Create an console app. In the program.cs file, insert the code below:
internal class Program {
static async Task<int> Main(string[] args) {
await using var host = new CommandHost("Sample Command Line Application")
.RegisterServices(RegisterServices)
// this method is generated by source generator
// this line will not compile before the creation of the first command
.AddCommands()
.Parse(args)
.Build();
return await host.InvokeAsync();
}
static void RegisterServices(ParseResult result, IConfiguration configuration, IServiceCollection services) {
// RegisterCommands method is generated by codegen
services.RegisterCommands();
// register the rest of the depencency here
...
}
}
Define a Verb
[Verb<HelloWorldCommandHandler>("hello", Description = "The HelloWorld command")]
public record class HelloWorldParams {
[Option]
public required string Name { get; init; }
}
Create the Command Handler
The Command Handler has to inherit interface: IAsyncCommandHandler
public class HelloWorldCommandHandler : IAsyncCommandHandler {
ParseResult result;
HelloWorldParams parameters;
ILogger logger;
public HelloWorldCommandHandler(ParseResult result, HelloWorldParams parameters, ILogger<HelloWorldCommandHandler> logger) {
this.result = result;
this.parameters = parameters;
this.logger = logger;
}
public async Task<int> InvokeAsync(CancellationToken cancellationToken) {
// do your work here
Console.WriteLine($"{result} is invoked with {parameter}");
}
}
Run the app
Usage: dotnet run -- hello --name test