public static IHostBuilder ConfigureCommandLineApplication <TApp>( this IHostBuilder hostBuilder, string[] args, out CommandLineState applicationState) where TApp : class { var state = new CommandLineState(args); hostBuilder.ConfigureServices((_, services) => { services.AddSingleton <IUnhandledExceptionHandler, GlobalExceptionHandler>(); services.AddSingleton(PhysicalConsole.Singleton); services.TryAddSingleton(provider => { state.SetConsole(provider.GetRequiredService <IConsole>()); return(state); }); services .AddSingleton <IHostLifetime, CommandLineLifetime>() .AddSingleton <CommandLineContext>(state) .AddSingleton <ICommandLineService, CommandLineService <TApp> >(); }); applicationState = state; return(hostBuilder); }
/// <summary> /// Creates a new instance. /// </summary> /// <param name="logger">A logger</param> /// <param name="state">The command line state</param> /// <param name="serviceProvider">The DI service provider</param> public CommandLineService( ILogger <CommandLineService <T> > logger, CommandLineState state, IServiceProvider serviceProvider) { _logger = logger; _state = state; logger.LogDebug( "Constructing CommandLineApplication<{type}> with args [{args}]", typeof(T).FullName, string.Join(",", state.Arguments)); _application = new CommandLineApplication <T>(state.Console, state.WorkingDirectory); _application .Conventions .UseDefaultConventions() .UseConstructorInjection(serviceProvider); foreach (var convention in serviceProvider.GetServices <IConvention>()) { _application.Conventions.AddConvention(convention); } }