Class CommandHost
- Namespace
- Albatross.CommandLine
- Assembly
- Albatross.CommandLine.dll
Host for running commands
await using var host = new CommandHost("Sample Command Line Application")
.RegisterServices((result, config, services) => {...})
.AddCommands()
.Parse(args)
.ConfigureHost((result, builder) => {...})
.Build();
return await host.InvokeAsync();
public class CommandHost : IAsyncDisposable
- Inheritance
-
CommandHost
- Implements
- Inherited Members
- Extension Methods
Constructors
CommandHost(string)
Creates a new command host with the specified description for the root command.
public CommandHost(string description)
Parameters
descriptionstringThe description to display in help text for the root command.
Properties
CommandBuilder
Gets the command builder used to configure the command hierarchy.
public CommandBuilder CommandBuilder { get; }
Property Value
RequiredResult
Gets the parse result. Throws if Parse(string[]) has not been called.
public ParseResult RequiredResult { get; }
Property Value
Methods
Build()
Builds the host and creates a service scope for command execution.
public CommandHost Build()
Returns
- CommandHost
The command host for method chaining.
ConfigureApplication(Action<ParseResult, IServiceProvider>)
Registers an action to configure the application after the host is built.
public CommandHost ConfigureApplication(Action<ParseResult, IServiceProvider> action)
Parameters
actionAction<ParseResult, IServiceProvider>An action to configure the application with access to the parse result and service provider.
Returns
- CommandHost
The command host for method chaining.
ConfigureHost(Action<IHostBuilder>)
Configures the underlying host builder.
public CommandHost ConfigureHost(Action<IHostBuilder> configure)
Parameters
configureAction<IHostBuilder>An action to configure the host builder.
Returns
- CommandHost
The command host for method chaining.
ConfigureHost(Action<ParseResult, IHostBuilder>)
Configures the underlying host builder with access to the parse result. Must be called after Parse(string[]).
public CommandHost ConfigureHost(Action<ParseResult, IHostBuilder> configure)
Parameters
configureAction<ParseResult, IHostBuilder>An action to configure the host builder with the parse result.
Returns
- CommandHost
The command host for method chaining.
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
GetServiceProvider()
Gets the scoped service provider. Throws if Build() has not been called.
public IServiceProvider GetServiceProvider()
Returns
- IServiceProvider
The scoped service provider.
InvokeAsync()
Invokes the parsed command asynchronously.
public Task<int> InvokeAsync()
Returns
Parse(string[])
Command Hierarchy should be built before calling Parse
public CommandHost Parse(string[] args)
Parameters
argsstring[]
Returns
RegisterServices(Action<ParseResult, IServiceCollection>)
Registers services with the dependency injection container. Must be called after Parse(string[]).
public CommandHost RegisterServices(Action<ParseResult, IServiceCollection> action)
Parameters
actionAction<ParseResult, IServiceCollection>An action to register services with access to the parse result.
Returns
- CommandHost
The command host for method chaining.