Table of Contents

Constructor CommandAppExecutor

Namespace
Ploch.CommandLine.Spectre
Assembly
Ploch.CommandLine.Spectre.dll

CommandAppExecutor(ICommandApp, CancellationToken)

Implements the ICommandAppExecutor interface to execute command-line applications.

public CommandAppExecutor(ICommandApp commandApp, CancellationToken cancellationToken)

Parameters

commandApp ICommandApp

The command application to execute.

cancellationToken CancellationToken

The token handed to Spectre, and through it to every command. Create(params IEnumerable<string>) cancels the source behind this token when the user interrupts the application, so a command that honours its CancellationToken stops when they do.

Remarks

The token is taken rather than the CancellationTokenSource behind it. This type only ever observes cancellation, it never requests it, so it has no use for the source. Taking the token also decouples execution from the source's lifetime: Token throws ObjectDisposedException once the source is disposed, whereas a token captured beforehand stays usable.

An executor permits one execution attempt. An attempt may build the host behind the type registrar, and Build() cannot be called again, so every attempt consumes the executor whatever its outcome: a second Run(params IEnumerable<string>) or RunAsync(params IEnumerable<string>) is rejected up front with an InvalidOperationException. Running again takes a new executor over a freshly configured command application; ConfigureCommandApp(Action<IConfigurator>) does both when the application was built through AppBuilder. An executor constructed directly wraps an Spectre.Console.Cli.ICommandApp the caller owns, so the caller configures a new one.

For an executor returned by ConfigureCommandApp(Action<IConfigurator>), a run that reaches command resolution builds the host for dependency injection, configuration and logging — a parse failure or --version returns before any host exists. The library never starts the host, so a registered IHostedService does not run, and a host that was built is disposed when the run ends. See the remarks on ConfigureCommandApp(Action<IConfigurator>).