SpectreConsole.Extensions 0.3.0

dotnet add package SpectreConsole.Extensions --version 0.3.0                
NuGet\Install-Package SpectreConsole.Extensions -Version 0.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SpectreConsole.Extensions" Version="0.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SpectreConsole.Extensions --version 0.3.0                
#r "nuget: SpectreConsole.Extensions, 0.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 from appsettings.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 the Build method and Configure 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.0 87 12/5/2024
0.2.0 106 10/29/2024
0.1.0 245 4/21/2023