@@ -35,7 +35,7 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
3535 CancellationToken cancellationToken = default )
3636 where TApp : class
3737 {
38- return await RunCommandLineApplicationAsync < TApp > ( hostBuilder , args , app => { } , cancellationToken ) ;
38+ return await RunCommandLineApplicationAsync < TApp > ( hostBuilder , args , null , cancellationToken ) ;
3939 }
4040
4141 /// <summary>
@@ -56,18 +56,7 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
5656 CancellationToken cancellationToken = default )
5757 where TApp : class
5858 {
59- var state = new CommandLineState ( args ) ;
60- hostBuilder . Properties [ typeof ( CommandLineState ) ] = state ;
61- hostBuilder . ConfigureServices (
62- ( context , services )
63- =>
64- {
65- services . AddCommonServices ( state ) ;
66- services . AddSingleton < ICommandLineService , CommandLineService < TApp > > ( ) ;
67- services . AddSingleton ( configure ) ;
68- } ) ;
69-
70- using var host = hostBuilder . Build ( ) ;
59+ using var host = hostBuilder . UseCommandLineApplication < TApp > ( args , configure ) . Build ( ) ;
7160 return await host . RunCommandLineApplicationAsync ( cancellationToken ) ;
7261 }
7362
@@ -88,16 +77,14 @@ public static async Task<int> RunCommandLineApplicationAsync(
8877 Action < CommandLineApplication > configure ,
8978 CancellationToken cancellationToken = default )
9079 {
80+ configure ??= _ => { } ;
9181 var state = new CommandLineState ( args ) ;
9282 hostBuilder . Properties [ typeof ( CommandLineState ) ] = state ;
93- hostBuilder . ConfigureServices (
94- ( context , services )
95- =>
96- {
97- services . AddCommonServices ( state ) ;
98- services . AddSingleton < ICommandLineService , CommandLineService > ( ) ;
99- services . AddSingleton ( configure ) ;
100- } ) ;
83+ hostBuilder . ConfigureServices ( ( context , services ) =>
84+ services
85+ . AddCommonServices ( state )
86+ . AddSingleton < ICommandLineService , CommandLineService > ( )
87+ . AddSingleton ( configure ) ) ;
10188
10289 using var host = hostBuilder . Build ( ) ;
10390 return await host . RunCommandLineApplicationAsync ( cancellationToken ) ;
@@ -131,50 +118,32 @@ public static IHostBuilder UseCommandLineApplication<TApp>(
131118 Action < CommandLineApplication < TApp > > configure )
132119 where TApp : class
133120 {
134- configure ??= app => { } ;
121+ configure ??= _ => { } ;
135122 var state = new CommandLineState ( args ) ;
136123 hostBuilder . Properties [ typeof ( CommandLineState ) ] = state ;
137- hostBuilder . ConfigureServices (
138- ( context , services ) =>
139- {
140- services
141- . TryAddSingleton < StoreExceptionHandler > ( ) ;
142- services
143- . TryAddSingleton < IUnhandledExceptionHandler > ( provider => provider . GetRequiredService < StoreExceptionHandler > ( ) ) ;
144- services
145- . AddSingleton < IHostLifetime , CommandLineLifetime > ( )
146- . TryAddSingleton ( PhysicalConsole . Singleton ) ;
147- services
148- . AddSingleton ( provider =>
149- {
150- state . SetConsole ( provider . GetService < IConsole > ( ) ) ;
151- return state ;
152- } )
153- . AddSingleton < CommandLineContext > ( state )
154- . AddSingleton < ICommandLineService , CommandLineService < TApp > > ( ) ;
155- services
156- . AddSingleton ( configure ) ;
157- } ) ;
124+ hostBuilder . ConfigureServices ( ( context , services ) =>
125+ services
126+ . AddCommonServices ( state )
127+ . AddSingleton < ICommandLineService , CommandLineService < TApp > > ( )
128+ . AddSingleton ( configure ) ) ;
158129
159130 return hostBuilder ;
160131 }
161132
162- private static void AddCommonServices ( this IServiceCollection services , CommandLineState state )
133+ private static IServiceCollection AddCommonServices ( this IServiceCollection services , CommandLineState state )
163134 {
164- services
165- . TryAddSingleton < StoreExceptionHandler > ( ) ;
166- services
167- . TryAddSingleton < IUnhandledExceptionHandler > ( provider => provider . GetRequiredService < StoreExceptionHandler > ( ) ) ;
135+ services . TryAddSingleton < StoreExceptionHandler > ( ) ;
136+ services . TryAddSingleton < IUnhandledExceptionHandler > ( provider => provider . GetRequiredService < StoreExceptionHandler > ( ) ) ;
137+ services . TryAddSingleton ( PhysicalConsole . Singleton ) ;
168138 services
169139 . AddSingleton < IHostLifetime , CommandLineLifetime > ( )
170- . TryAddSingleton ( PhysicalConsole . Singleton ) ;
171- services
172140 . AddSingleton ( provider =>
173141 {
174142 state . SetConsole ( provider . GetService < IConsole > ( ) ) ;
175143 return state ;
176144 } )
177145 . AddSingleton < CommandLineContext > ( state ) ;
146+ return services ;
178147 }
179148 }
180149}
0 commit comments