using System.CommandLine; using Canon.Console.Models; using Canon.Console.Services; using Canon.Core.Abstractions; using Canon.Core.LexicalParser; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace Canon.Console.Extensions; public static class RootCommandExtensions { public static void SetCompile(this RootCommand rootCommand) { Option sourceFilenameOption = new("--input", "The source pascal filename") { IsRequired = true }; sourceFilenameOption.AddAlias("-i"); rootCommand.AddOption(sourceFilenameOption); rootCommand.SetHandler(async (context) => { string? sourceFilename = context.ParseResult.GetValueForOption(sourceFilenameOption); if (sourceFilename is null) { System.Console.WriteLine("Error: please provide source filename with option '-i'."); return; } HostApplicationBuilder builder = Host.CreateApplicationBuilder(); builder.Logging.ClearProviders(); builder.Logging.Services.AddSingleton(); builder.Services.AddSingleton( _ => new CompilerOption { SourceFilename = sourceFilename }); builder.Services.AddTransient(); builder.Services.AddSingleton(_ => GeneratedGrammarParser.Instance); builder.Services.AddHostedService(); using IHost host = builder.Build(); await host.RunAsync(); }); } }