Skip to content

Add Async Methods on CommandLineApplication that accept CancellationToken #153

@bloudraak

Description

@bloudraak

Describe the bug
Consider the following code snippet:

public async Task<int> ExecuteAsync<TCommand>(string[] args, CancellationToken cancellationToken = default) where TCommand:class
{
    cancellationToken.ThrowIfCancellationRequested();
    
    var app = new CommandLineApplication<TCommand>();
    try
    {
        app.Conventions
            .UseDefaultConventions()
            .UseConstructorInjection(Services);

        return app.Execute(args);
        
    }
    catch (CommandParsingException e)
    {
        await app.Out.WriteLineAsync(e.Message);
        app.ShowHelp();
        return -1;
    }
}

The API doesn't expose ExecuteAsync(args, cancellationToken).

To Reproduce
There is no ExecuteAsync method that takes a cancellation token, which in turn complicates matters when TCommand implements an ExecuteAsync method, which uses a cancellation token.

Expected behavior
The expectation is that all methods on CommandLineApplication exposes async methods that accepts a cancellation token, so that a long running application can be cancelled.

Additional context
The need exists to signal to the instance of TCommand that is running asynchronously that processing should be cancelled for whatever reason, e.g. the user pressed CTRL+C.

Metadata

Metadata

Assignees

Labels

enhancementhelp wantedWe would be willing to take a well-written PR to help fix this.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions