SpectreConsole.Extensions
0.3.0
dotnet add package SpectreConsole.Extensions --version 0.3.0
NuGet\Install-Package SpectreConsole.Extensions -Version 0.3.0
<PackageReference Include="SpectreConsole.Extensions" Version="0.3.0" />
paket add SpectreConsole.Extensions --version 0.3.0
#r "nuget: SpectreConsole.Extensions, 0.3.0"
// Install SpectreConsole.Extensions as a Cake Addin #addin nuget:?package=SpectreConsole.Extensions&version=0.3.0 // Install SpectreConsole.Extensions as a Cake Tool #tool nuget:?package=SpectreConsole.Extensions&version=0.3.0
Spectre.Console Extensions
This project contains some extensions for the Spectre.Console CLI project. It adds a CommandAppBuilder which enables features like Dependency injection.
Requirements
The project supports .NET Standard 2.0, .NET 8 and .NET 9.
Installation
This project is available on NuGet.
It can be installed using the dotnet add package
command or the NuGet wizard on your favourite IDE.
dotnet add package SpectreConsole.Extensions
Features
By default the CommandAppBuilder
allows you to register types in the .NET Dependency injection and manage the configuration based on appsettings.json
file and user secrets.
It expose also an Environment
property which allows you to perform action based on the standard .NET Environment values. This values are specified via the DOTNET_ENVIRONMENT
environment variable.
Usage
In your Program.cs
, you can create a CommandAppBuilder
class and then register your services or manage your configuration:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Spectre.Console.Extensions;
var builder = CommandAppBuilder.Create();
// Register your services with the Services property. For example:
builder.Services.AddSingleton<IMyService, MyService>();
// Configure types reading data from appsettings.json or user secrets
builder.Services.Configure<MyOption>(builder.Configuration.GetSection("MyOption"));
// You can perform actions based on the Environment
if (builder.Environment.IsDevelopment())
{
// Register types based on the current environment
builder.Services.AddSingleton<IDevelopmentService, DevelopmentService>();
}
// Create the CommandApp instance
var app = builder.Build();
app.Configure(config =>
{
// Your app configuration
config.AddCommand<MyCommand>("mycommand");
});
// Run the application
return app.Run(args);
Explanation
- Register Services: Use
builder.Services
to register your services with the dependency injection container. - Configure Options: Use
builder.Configuration
to configure options fromappsettings.json
or user secrets. - Environment-based Registration: Use
builder.Environment
to register services conditionally based on the environment. - Build and Configure CommandApp: Create and configure the
CommandApp
instance using theBuild
method andConfigure
method.
MyOption Class
Ensure you have a class MyOption
defined to hold configuration values:
public class MyOption
{
public string Value { get; set; } = string.Empty;
}
Sample Command
Define a sample command to be used in the CommandApp
:
internal class MyCommand : Command<MyCommand.CommandSettings>
{
private readonly IMyService _service;
private readonly MyOption _option;
public MyCommand(IMyService service, IOptions<MyOption> options)
{
_service = service ?? throw new ArgumentNullException(nameof(service));
_option = options.Value;
}
public override int Execute(CommandContext context, CommandSettings settings)
{
AnsiConsole.MarkupLine($"Service: {_service.SayHello()}");
AnsiConsole.MarkupLine($"Option: {_option.Value}");
return 0;
}
internal class CommandSettings : CommandSettings
{
[CommandOption("-n|--name")]
public string Name { get; set; } = string.Empty;
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 9.0.0)
- Microsoft.Extensions.Configuration.Json (>= 9.0.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- Spectre.Console.Cli (>= 0.49.1)
-
net8.0
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 9.0.0)
- Microsoft.Extensions.Configuration.Json (>= 9.0.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- Spectre.Console.Cli (>= 0.49.1)
-
net9.0
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 9.0.0)
- Microsoft.Extensions.Configuration.Json (>= 9.0.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- Spectre.Console.Cli (>= 0.49.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.