wabbajack/Wabbajack.CLI/CommandLineBuilder.cs

65 lines
1.6 KiB
C#
Raw Normal View History

2022-10-01 00:46:15 +00:00
using System;
2021-09-27 12:42:46 +00:00
using System.Collections.Generic;
using System.CommandLine;
2022-10-01 00:46:15 +00:00
using System.ComponentModel.Design;
using System.Linq;
2021-09-27 12:42:46 +00:00
using System.Threading.Tasks;
2022-10-01 00:46:15 +00:00
using Microsoft.Extensions.DependencyInjection;
2021-09-27 12:42:46 +00:00
using Wabbajack.CLI.Verbs;
using Wabbajack.Services.OSIntegrated;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.CLI;
2022-10-01 00:46:15 +00:00
public partial class CommandLineBuilder
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private readonly IConsole _console;
private readonly IEnumerable<IVerb> _verbs;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public CommandLineBuilder(IEnumerable<IVerb> verbs, IConsole console, LoggingRateLimiterReporter _)
{
_console = console;
_verbs = verbs;
}
2021-09-27 12:42:46 +00:00
2022-10-01 00:46:15 +00:00
static CommandLineBuilder()
{
RegisterAll();
}
2021-10-23 16:51:17 +00:00
public async Task<int> Run(string[] args)
{
var root = new RootCommand();
foreach (var verb in _verbs)
root.Add(verb.MakeCommand());
2022-01-18 22:58:54 +00:00
2021-10-23 16:51:17 +00:00
return await root.InvokeAsync(args);
2021-09-27 12:42:46 +00:00
}
2022-10-01 00:46:15 +00:00
private static List<(Type, Func<Command>)> _commands { get; set; } = new();
public static IEnumerable<Type> Verbs => _commands.Select(c => c.Item1);
public static void RegisterCommand<T>(Func<Command> makeCommand)
{
_commands.Add((typeof(T), makeCommand));
}
}
public record VerbDefinition()
{
}
public static class CommandLineBuilderExtensions
{
public static IServiceCollection AddCommands(this IServiceCollection services)
{
services.AddSingleton<CommandLineBuilder>();
foreach (var verb in CommandLineBuilder.Verbs)
{
services.AddSingleton(typeof(IVerb), verb);
}
return services;
}
2021-09-27 12:42:46 +00:00
}