Spectre.Console.Cli注入服务的几种姿势

Spectre.Console大家可能都不陌生,写控制台程序美化还是不错的,支持着色,表格,图标等相当nice,如果对这个库不熟悉我强烈推荐你了解一下,对于写一些CLI小工具还是相当方便的, 本文主要讲讲 Spectre.Console.Cli的服务注入, TA是 Spectre.Console 库的一部分,用于创建命令行界面(CLI)应用程序。它提供了一个强大且易于使用的API来定义命令、参数和选项,同时支持 Spectre.Console 的丰富输出格式化功能。

一个官方极简的例子,定义一个GreetCommand:

csharp 复制代码
public class GreetCommand : Command<GreetCommand.Settings>
{
    public class Settings : CommandSettings
    {
        [CommandArgument(0, "<name>")]
        [Description("The name of the person to greet.")]
        public string Name { get; set; }

        [CommandOption("-r|--repeat <times>")]
        [Description("The number of times to repeat the greeting.")]
        [DefaultValue(1)]
        public int Repeat { get; set; }
    }
    public override int Execute(CommandContext context, Settings settings)
    {
        for (int i = 0; i < settings.Repeat; i++)
        {
            Console.WriteLine($"Hello, {settings.Name}!");
        }
        return 0;
    }
}

接下来,在程序的入口点配置Command

csharp 复制代码
public class Program
{
    public static int Main(string[] args)
    {
        var app = new CommandApp();
        app.Configure(config =>
        {
            config.AddCommand<GreetCommand>("greet");
        });
        return app.Run(args);
    }
}

对于Spectre.Console.Cli的常规使用我这里不做过多介绍,感兴趣的同学可以移步官方文档,本文主要讲一下在CLI程序中如何注入服务

那么我们需要在GreetCommand中注入服务应该怎么做呢? 比如下面的一个服务:

csharp 复制代码
public class HelloService(ILogger<HelloService> logger)
{
    public Task<string> SayHello(string name, int age)
    {
        //注入的logger
        logger.LogInformation("SayHello called with name:{name},age:{age}", name, age);
        return Task.FromResult($"Hello,My name is {name}, I`m {age} years old!");
    }
}

其实Spectre.Console.Cli内置了最简单的方式,我们只需要在app.Configure中完成:

csharp 复制代码
var services = new ServiceCollection();
//添加服务
services.AddSingleton<HelloService>();
//添加日志
services.AddLogging(config =>
{
    config.AddConsole();
});
var sp = services.BuildServiceProvider();

app.Configure(config =>
{
    //添加Commands
    config.AddCommand<OneCommand>("one");
    config.AddCommand<AnotherCommand>("another");
    //注册Services
    config.Settings.Registrar.RegisterInstance(sp.GetRequiredService<HelloService>());
});

注册的服务就可以直接使用了:

csharp 复制代码
public class HelloCommand(HelloService helloService) : AsyncCommand<HelloCommand.HelloSettings>
{
    private readonly HelloService _helloService = helloService;
    public class HelloSettings : CommandSettings
    {
        [CommandArgument(0, "<name>")]
        [Description("The target to say hello to.")]
        public string Name { get; set; } = null!;
    }
    public override async Task<int> ExecuteAsync(CommandContext context, HelloSettings settings)
    {
        var message = await _helloService.SayHello(settings.Name, settings.Age);
        AnsiConsole.MarkupLine($"[blue]{message}[/]");
        return 0;
    }
}

另外的一个注入方式是实现ITypeRegistrar,官方提供MSDI的用例,自己也可以实现Autofac等其他DI,下面是MSDI的实现:

csharp 复制代码
namespace Infrastructure
{
    public sealed class MsDITypeRegistrar(IServiceCollection services) : ITypeRegistrar
    {
        private readonly IServiceCollection _services =
            services ?? throw new ArgumentNullException(nameof(services));
        public ITypeResolver Build()
        {
            return new TypeResolver(_services.BuildServiceProvider());
        }
        public void Register(Type service, Type implementation)
        {
            _services.AddSingleton(service, implementation);
        }
        public void RegisterInstance(Type service, object implementation)
        {
            _services.AddSingleton(service, implementation);
        }
        public void RegisterLazy(Type service, Func<object> factory)
        {
            _services.AddSingleton(service, (provider) => factory());
        }
    }
    internal sealed class TypeResolver(IServiceProvider provider) : ITypeResolver
    {
        public object? Resolve(Type? type)
        {
            if (provider is null || type is null)
            {
                return null;
            }
            return ActivatorUtilities.GetServiceOrCreateInstance(provider, type);
        }
    }
}

使用的话只需要实例化CommandApp时候传入MsDITypeRegistrar即可:

csharp 复制代码
var services = new ServiceCollection();
//添加服务...

var app = new CommandApp(new MsDITypeRegistrar(services));
app.Configure(config =>
{
   //...
});
return app.Run(args);

除了上面的方式,我们其实还可以使用ICommandInterceptor切面的方式来完成注入的操作:

下面我们定义一个AutoDIAttribute特性,实现一个AutoDIInterceptor的拦截器,后者主要给标记了AutoDI的属性服务赋值

csharp 复制代码
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class AutoDIAttribute : Attribute{ }

/// <summary>
/// 自动注入的拦截器
/// </summary>
internal class AutoDIInterceptor(IServiceProvider serviceProvider) : ICommandInterceptor
{
    public void Intercept(CommandContext context, CommandSettings settings)
    {
        var type = settings.GetType();
        var properties = type.GetProperties();
        foreach (var property in properties)
        {
            var isAutoInject = property.GetCustomAttributes<AutoDIAttribute>(true).Any();
            if (isAutoInject)
            {
                var service = ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, property.PropertyType);
                property.SetValue(settings, service);
            }
        }
    }
}

接下来在CommandSettings中标记需要自动注入服务的属性,如下面的HelloService:

csharp 复制代码
internal class AutoDICommand : AsyncCommand<AutoDICommand.AnotherInjectSettings>
{
    public class AnotherInjectSettings : CommandSettings
    {
        /// <summary>
        /// 使用切面装载的服务
        /// </summary>
        [AutoDI]
        public HelloService HelloService { get; set; } = null!;

        [Description("user name")]
        [DefaultValue("vipwan"), CommandOption("-n|--name")]
        public string Name { get; set; } = null!;

        [Description("user age")]
        [DefaultValue(12), CommandOption("-a|--age")]
        public int Age { get; set; }
    }

    public override async Task<int> ExecuteAsync(CommandContext context, AnotherInjectSettings settings)
    {
        var message = await settings.HelloService.SayHello(settings.Name, settings.Age);
        AnsiConsole.MarkupLine($"[green]{message}[/]");
        return 0;
    }
}

然后在app.Configure中使用AutoDIInterceptor切面:

csharp 复制代码
var services = new ServiceCollection();
//添加服务
services.AddSingleton<HelloService>();
var sp = services.BuildServiceProvider();

app.Configure(config =>
{
    //设置自动注入的拦截器
    config.SetInterceptor(new AutoDIInterceptor(sp));
    config.AddCommand<AutoDICommand>("di");
    //...
});

然后测试运行程序:

bash 复制代码
dotnet run -- di -n "vipwan"

大功告成:

24.cnblogs.com/blog/127598/202407/127598-20240724180421239-1298412615.png)

以上就介绍了几种在Spectre.Console.Cli注入服务的方式,当然没有最优的只有最适合自己的,如果代码存在不足,或者你有更好的建议 欢迎留言交流!

相关推荐
AI自动化工坊2 小时前
GitHub CLI统一AI编码技能管理:技术规范与工程实践指南
人工智能·github·agent·cli
曲幽4 天前
我用fastapi-scaff搭了个项目,两天工期缩到两小时,老板以为我开挂了
python·api·fastapi·web·celery·cli·db·alembic·fastapi-scaff
大强同学12 天前
Obsidian CLI + Claude Code = 王炸组合
人工智能·windows·ai编程·cli
PieroPc12 天前
一个为 AI 助手设计的进销存管理系统,内置完整的 CLI 命令接口,让 AI 可以通过自然语言或命令行直接操作库存。技术栈 FastAPI+Html
人工智能·html·fastapi·cli
MicrosoftReactor14 天前
技术速递|使用 Copilot CLI 中的 /fleet 一次运行多个智能体
人工智能·copilot·cli·智能体
带刺的坐椅16 天前
SolonCode CLI v2026.4.5 发布(编码智能体)
ai·llm·ai编程·cli·claudecode·opencode·sloncode
小马过河R16 天前
AI时代下的CLI优势与MCP对比
人工智能·ai·语言模型·agent·cli·智能体·mcp
带刺的坐椅17 天前
CLI 编程代理横向分析报告
ai编程·cli·claudecode·opencode·soloncode
MicrosoftReactor18 天前
技术速递|从想法到拉取请求:使用 GitHub Copilot CLI 构建的实用指南
github·copilot·cli
Ants19 天前
我用 AI 辅助开发了一系列小工具(1):文件提取工具
app·cli