Example #1
0
        /// <summary>
        ///     Runs an instance of <see cref="CommandLineApplication" /> to provide
        ///     command line parsing on the given <paramref name="args" />.  This method should be the primary approach
        ///     taken for command line applications which use the Builder API for configuration.
        /// </summary>
        /// <param name="hostBuilder">This instance</param>
        /// <param name="args">The command line arguments</param>
        /// <param name="cancellationToken">A cancellation token</param>
        /// <returns>A task whose result is the exit code of the application</returns>
        public static async Task <int> RunCommandLineApplicationAsync(
            this IHostBuilder hostBuilder, string[] args, CancellationToken cancellationToken = default)
        {
            var exceptionHandler = new StoreExceptionHandler();
            var state            = new CommandLineState(args);

            hostBuilder.ConfigureServices(
                (context, services)
                =>
            {
                services
                .TryAddSingleton <IUnhandledExceptionHandler>(exceptionHandler);
                services
                .AddSingleton <IHostLifetime, CommandLineLifetime>()
                .TryAddSingleton(PhysicalConsole.Singleton);
                services
                .AddSingleton(provider =>
                {
                    state.SetConsole(provider.GetService <IConsole>());
                    return(state);
                })
                .AddSingleton <CommandLineContext>(state)
                .AddSingleton <ICommandLineService, CommandLineService>();
            });

            using var host = hostBuilder.Build();
            await host.RunAsync(cancellationToken);

            if (exceptionHandler.StoredException != null)
            {
                ExceptionDispatchInfo.Capture(exceptionHandler.StoredException).Throw();
            }

            return(state.ExitCode);
        }
Example #2
0
        /// <summary>
        /// Configures an instance of <see cref="IHostBuilder"/> for running a <see cref="CommandLineApplication"/>.
        /// </summary>
        /// <typeparam name="TApp"></typeparam>
        /// <param name="hostBuilder"></param>
        /// <param name="args"></param>
        /// <param name="applicationState"></param>
        /// <returns></returns>
        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(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);
        }
Example #3
0
 private static IServiceCollection AddCommonServices(this IServiceCollection services, CommandLineState state)
 {
     services.TryAddSingleton <StoreExceptionHandler>();
     services.TryAddSingleton <IUnhandledExceptionHandler>(provider => provider.GetRequiredService <StoreExceptionHandler>());
     services.TryAddSingleton(PhysicalConsole.Singleton);
     services
     .AddSingleton <IHostLifetime, CommandLineLifetime>()
     .AddSingleton(provider =>
     {
         state.SetConsole(provider.GetRequiredService <IConsole>());
         return(state);
     })
     .AddSingleton <CommandLineContext>(state);
     return(services);
 }
Example #4
0
        /// <summary>
        ///     Runs an instance of <typeparamref name="TApp" /> using <see cref="CommandLineApplication" /> to provide
        ///     command line parsing on the given <paramref name="args" />.  This method should be the primary approach
        ///     taken for command line applications.
        /// </summary>
        /// <typeparam name="TApp">The type of the command line application implementation.</typeparam>
        /// <param name="hostBuilder">This instance.</param>
        /// <param name="args">The command line arguments.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>A task whose result is the exit code of the application.</returns>
        public static async Task <int> RunCommandLineAsync <TApp>(
            this IHostBuilder hostBuilder,
            string[] args,
            CancellationToken cancellationToken = default) where TApp : class
        {
            var exceptionHandler = new StoreExceptionHandler();
            var state            = new CommandLineState(args);

            hostBuilder.ConfigureServices(
                (context, services)
                =>
            {
                services
                .TryAddSingleton <IUnhandledExceptionHandler>(exceptionHandler);
                services
                .AddSingleton <IHostLifetime, CommandLineLifetime>()
                .TryAddSingleton(PhysicalConsole.Singleton);
                services
                .AddSingleton(provider =>
                {
                    state.SetConsole(provider.GetService <IConsole>());
                    return(state);
                })
                .AddSingleton <CommandLineContext>(state)
                .AddSingleton <ICommandLineService, CommandLineService <TApp> >();
            });

            hostBuilder.UseSerilog((context, logger) =>
            {
                var configuration = context.Configuration;

                logger.ReadFrom.Configuration(configuration)
                .Enrich.FromLogContext()
                .WriteTo.Console();
            });

            using var host = hostBuilder.Build();
            await host.RunAsync(cancellationToken);

            if (exceptionHandler.StoredException != null)
            {
                ExceptionDispatchInfo.Capture(exceptionHandler.StoredException).Throw();
            }

            return(state.ExitCode);
        }