I’ve been using and writing about the Spectre.Console project to easily build console applications for a long time now. Read about building Spectre.Console apps here.
As I’ve been working with this library, I have duplicated code in various projects that should really be shared… like the ITypeRegistrar
and ITypeResolver
for different Dependency Injection (DI) frameworks. And there are other people that could benefit from using common implementations of these integrations.
Therefore, I have created the D20Tek.Spectre.Console.Extensions project. This project currently contains implementations of ITypeRegistrar
and ITypeResolver
for the following DI frameworks:
- Microsoft.Extensions.DependencyInjection
- Autofac
- LightInject
- Ninject
- SimpleInjector
And the CommandAppBuilder
fluent API to create, configure, and run console applications.
Installating Spectre.Console.Extensions Package
This library is a NuGet package so it is easy to add to your Spectre console project. To install this package into your solution, you can use the NuGet Package Manager. In PM, please use the following command:
PM > Install-Package D20Tek.Spectre.Console.Extensions -Version 1.0.4
To install in the Visual Studio UI, go to the Tools menu > “Manage NuGet Packages”. Then search for D20Tek.Spectre.Console.Extensions and install it from there.
Read more about the current release in the project Release Notes.
Using Spectre.Console.Extensions
To use these extensions in your project, first create a Startup.cs
file in your project.
//---------------------------------------------------------------------------------------------------------------------
// Copyright (c) d20Tek. All rights reserved.
//---------------------------------------------------------------------------------------------------------------------
using D20Tek.Samples.Common.Commands;
using D20Tek.Samples.Common.Services;
using D20Tek.Spectre.Console.Extensions;
using Spectre.Console.Cli;
namespace DependencyInjection.Cli
{
internal class Startup : StartupBase
{
public override void ConfigureServices(ITypeRegistrar registrar)
{
// register services here...
registrar.Register(typeof(IDisplayWriter), typeof(ConsoleDisplayWriter));
}
public override IConfigurator ConfigureCommands(IConfigurator config)
{
config.CaseSensitivity(CaseSensitivity.None);
config.SetApplicationName("DependencyInjection.Cli");
config.ValidateExamples();
config.AddCommand<DefaultCommand>("default")
.WithDescription("Default command that displays some text.")
.WithExample(new[] { "default", "--verbose", "high" });
return config;
}
}
}
Then, update the Program.cs
file to use the CommandAppBuilder
.
//---------------------------------------------------------------------------------------------------------------------
// Copyright (c) d20Tek. All rights reserved.
//---------------------------------------------------------------------------------------------------------------------
using D20Tek.Samples.Common.Commands;
using D20Tek.Spectre.Console.Extensions;
namespace DependencyInjection.Cli
{
public class Program
{
public static async Task<int> Main(string[] args)
{
return await new CommandAppBuilder()
.WithDIContainer()
.WithStartup<Startup>()
.WithDefaultCommand<DefaultCommand>()
.Build()
.RunAsync(args);
}
}
}
Finally build and run your console app to see the command run.
Full Sample Code
For more detailed examples on how to use D20Tek.Spectre.Console.Extensions, please review the following sample projects:
- Basic Cli with DI – has a full listing of code to use DI in Spectre.Console.
- DependencyInjection.Cli – is a more elaborate use of Microsoft.Extensions.DependencyInjection registrar and resolver. Along with using the CommandAppBuilder to remove some of the creation complexity.
- Autofac.Cli – Use the Autofac DI framework to build type registrar and resolver.
- LightInject.Cli – Use the LightInject DI framework to build type registrar and resolver.
- Ninject.Cli – Uses the Ninject DI framework for the type registrar and resolver.
- SimpleInjector.Cli – Uses the SimpleInjector DI framework for the type registrar and resolver.
- NoDI.Cli – Uses the CommandAppBuilder to configure a console app that does not use a DI framework.
Coming Soon
This project is in progress, and we will build additional integrations for popular DI frameworks like Spring.NET and others. There are other integration points in Spectre.Console, like custom loggers, which we will also investigate.
Please try our extensions with your own Spectre.Console apps. And if you have any feedback or feature requests, please post them in our project issues.
Hey man, I’m glad to see you’re posting about Spectre again – and you’ve even started a project with some extensions. Nice!
In the meantime (since I commented your 3.11 wrap post) I got the chance to play around with Spectre too. Love it. Still wondering what’s best to achieve auto-completion. Didn’t play around with that yet 🙂
Keep blogging 🙂
LikeLike