Oakton 1.4.0


Next

Bootstrapping

Previous

Commands

Asynchronous Execution Edit on GitHub


New in Oakton 1.4 is the ability to write asynchronous commands and also to take advantage of the ability to use asynchronous Program.Main() method signatures in recent versions of .Net.

To write an asynchronous command, use the OaktonAsyncCommand<T> type as your base class for your command as shown below:


[Description("Say my name", Name = "say-async-name")]
public class AsyncSayNameCommand : OaktonAsyncCommand<SayName>
{
    public AsyncSayNameCommand()
    {
        Usage("Capture the users name").Arguments(x => x.FirstName, x => x.LastName);
    }

    public override async Task<bool> Execute(SayName input)
    {
        await Console.Out.WriteLineAsync($"{input.FirstName} {input.LastName}");

        return true;
    }
}

Likewise, to execute asynchronously from Program.Main(), there are new overloads on CommandExecutor for async:


static Task<int> Main(string[] args)
{
    var executor = CommandExecutor.For(_ =>
    {
        // Find and apply all command classes discovered
        // in this assembly
        _.RegisterCommands(typeof(Program).GetTypeInfo().Assembly);
    });

    return executor.ExecuteAsync(args);
}