command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

How to propagate custom CancellationToken?

Open sakno opened this issue 3 years ago • 4 comments

I'm using CommandLine library in embedded scenario where input and output are fully controlled by the host (and not associated with stdin and stdout). In this case, CancelOnProcessTermination makes no sense. Instead, I want to propagate cancellation token from the host. At the moment, it is not possible. InvocationContext.GetCancellationToken returning token associated with internal CTS. As a result, there is no way to cancel the operation by the host.

To address this issue, I would like to propose some changes in public API:

public static class ParserExtensions
{
    // added token as the last parameter
    public static int Invoke(this Parser parser, string commandLine, IConsole? console = null, CancellationToken token = default);
    public static int Invoke(this Parser parser, string[] args, IConsole? console = null, CancellationToken token = default);
    public static Task<int> InvokeAsync(this Parser parser, string commandLine, IConsole? console = null, CancellationToken token = default);
    public static Task<int> InvokeAsync(this Parser parser, string[] args, IConsole? console = null, CancellationToken token = default);
}

public static class CommandExtensions
{
    // added token as the last parameter
    public static int Invoke(this Command command, string[] args, IConsole? console = null, CancellationToken token = default);
    public static int Invoke(this Command command, string commandLine, IConsole? console = null, CancellationToken token = default);
    public static Task<int> InvokeAsync(this Command command, string[] args, IConsole? console = null, CancellationToken token = default);
    public static Task<int> InvokeAsync(this Command command, string commandLine, IConsole? console = null, CancellationToken token = default);
}

If the changes will be approved, I'm ready to make a PR.

sakno avatar Jun 30 '22 12:06 sakno

You raise a valid use case. This looks related to #1469 and #1502. Have you looked at those and do you think it would address this issue?

jonsequitur avatar Jul 01 '22 00:07 jonsequitur

Yeah, I didn't try hard enough to find a similar issues :( I see that PR is still open. Can I help somehow with that? Or PR is on the final stage of review?

sakno avatar Jul 01 '22 07:07 sakno

It's a little stale, potentially ready, and extra eyes on it are always welcome.

jonsequitur avatar Jul 01 '22 15:07 jonsequitur

@jonsequitur , review from my side is done. Also, I proposed a few changes to the original PR.

sakno avatar Jul 02 '22 09:07 sakno