mfgames-cil/examples/SampleTool/SampleToolModule.cs

50 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Linq;
using Autofac;
using MfGames.ToolBuilder;
namespace SampleTool;
/// <summary>
/// The Autofac module to pull in the components inside this assembly.
/// </summary>
public class SampleToolModule : Module
{
/// <inheritdoc />
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(this.GetType().Assembly).AsSelf().AsImplementedInterfaces();
builder
.Register(c =>
{
// Create the top-level command.
var root = new RootCommand
{
Name = "sample-tool",
Description = "A sample tool that demonstrates functionality",
};
// Add in the top-level commands.
var commandList = c.Resolve<IList<ITopCommand>>().Cast<Command>().ToList();
if (commandList.Count == 0)
{
throw new InvalidOperationException(
"Cannot create a tool without at least one command extending System.CommandLine.Command"
);
}
foreach (Command command in commandList)
{
root.AddCommand(command);
}
return root;
})
.AsSelf();
}
}