Skip to content

How to propagate custom CancellationToken? #1783

Closed
@sakno

Description

@sakno

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions