spectre.console icon indicating copy to clipboard operation
spectre.console copied to clipboard

enhanced async support

Open althunibat opened this issue 2 years ago • 1 comments

few notes / enhancements that are recommended when handling async commands in a cli application.

When using async command in cli, it is recommended to add cancelation token to be passed to "ExecuteAsync" method console methods both in and out directions / write and read to have async version similar to: System.Console.Out.WriteLineAsync & System.Console.In.ReadLineAsync.

Thanks


Please upvote :+1: this issue if you are interested in it.

althunibat avatar Dec 21 '22 18:12 althunibat

Related to/the same as: https://github.com/spectreconsole/spectre.console/issues/701, CLI Command RunAsync support passing in/propagating a CancelationToken

FrankRay78 avatar Mar 07 '24 09:03 FrankRay78

Can I have a go at this?

Call911plz avatar Sep 16 '25 21:09 Call911plz

I gave it a shot here. Sorry if I was supposed to talk about my implementations as I was making the changes, this is my first github contribution and I don't really know the etiquette.

Essentially, I just added in a CancellationToken parameter for the command classes and the internal classes that call them. Only the abstract ExecuteAsync for the AsyncCommand has a CancellationToken parameter so that the users can handle the cancellation logic as shown in src/Tests/Spectre.Console.Cli.Tests/Data/Commands/AsynchronousCommand.cs.

The tests are essentially the same except they generate a CancellationTokenSource and the exception functions set the cts to cancel.

https://github.com/Call911plz/spectre.console/tree/AddCancellationToken

Call911plz avatar Sep 21 '25 06:09 Call911plz

Closing as duplicate of #701. See also https://github.com/spectreconsole/spectre.console/pull/1904#issuecomment-3375870782

0xced avatar Oct 07 '25 08:10 0xced