22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
5- using System . Runtime . ExceptionServices ;
65using System . Threading ;
76using System . Threading . Tasks ;
87using McMaster . Extensions . CommandLineUtils ;
@@ -36,7 +35,7 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
3635 CancellationToken cancellationToken = default )
3736 where TApp : class
3837 {
39- return await RunCommandLineApplicationAsync < TApp > ( hostBuilder , args , null , cancellationToken ) ;
38+ return await RunCommandLineApplicationAsync < TApp > ( hostBuilder , args , app => { } , cancellationToken ) ;
4039 }
4140
4241 /// <summary>
@@ -57,40 +56,19 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
5756 CancellationToken cancellationToken = default )
5857 where TApp : class
5958 {
60- configure ??= app => { } ;
61- var exceptionHandler = new StoreExceptionHandler ( ) ;
6259 var state = new CommandLineState ( args ) ;
6360 hostBuilder . Properties [ typeof ( CommandLineState ) ] = state ;
6461 hostBuilder . ConfigureServices (
6562 ( context , services )
6663 =>
6764 {
68- services
69- . TryAddSingleton < IUnhandledExceptionHandler > ( exceptionHandler ) ;
70- services
71- . AddSingleton < IHostLifetime , CommandLineLifetime > ( )
72- . TryAddSingleton ( PhysicalConsole . Singleton ) ;
73- services
74- . AddSingleton ( provider =>
75- {
76- state . SetConsole ( provider . GetService < IConsole > ( ) ) ;
77- return state ;
78- } )
79- . AddSingleton < CommandLineContext > ( state )
80- . AddSingleton < ICommandLineService , CommandLineService < TApp > > ( ) ;
81- services
82- . AddSingleton ( configure ) ;
65+ services . AddCommonServices ( state ) ;
66+ services . AddSingleton < ICommandLineService , CommandLineService < TApp > > ( ) ;
67+ services . AddSingleton ( configure ) ;
8368 } ) ;
8469
8570 using var host = hostBuilder . Build ( ) ;
86- await host . RunAsync ( cancellationToken ) ;
87-
88- if ( exceptionHandler . StoredException != null )
89- {
90- ExceptionDispatchInfo . Capture ( exceptionHandler . StoredException ) . Throw ( ) ;
91- }
92-
93- return state . ExitCode ;
71+ return await host . RunCommandLineApplicationAsync ( cancellationToken ) ;
9472 }
9573
9674 /// <summary>
@@ -110,41 +88,33 @@ public static async Task<int> RunCommandLineApplicationAsync(
11088 Action < CommandLineApplication > configure ,
11189 CancellationToken cancellationToken = default )
11290 {
113- var exceptionHandler = new StoreExceptionHandler ( ) ;
11491 var state = new CommandLineState ( args ) ;
11592 hostBuilder . Properties [ typeof ( CommandLineState ) ] = state ;
11693 hostBuilder . ConfigureServices (
11794 ( context , services )
11895 =>
11996 {
120- services
121- . TryAddSingleton < IUnhandledExceptionHandler > ( exceptionHandler ) ;
122- services
123- . AddSingleton < IHostLifetime , CommandLineLifetime > ( )
124- . TryAddSingleton ( PhysicalConsole . Singleton ) ;
125- services
126- . AddSingleton ( provider =>
127- {
128- state . SetConsole ( provider . GetService < IConsole > ( ) ) ;
129- return state ;
130- } )
131- . AddSingleton < CommandLineContext > ( state )
132- . AddSingleton < ICommandLineService , CommandLineService > ( ) ;
133- services
134- . AddSingleton ( configure ) ;
97+ services . AddCommonServices ( state ) ;
98+ services . AddSingleton < ICommandLineService , CommandLineService > ( ) ;
99+ services . AddSingleton ( configure ) ;
135100 } ) ;
136101
137102 using var host = hostBuilder . Build ( ) ;
103+ return await host . RunCommandLineApplicationAsync ( cancellationToken ) ;
104+ }
138105
139- await host . RunAsync ( cancellationToken ) ;
140-
141- if ( exceptionHandler . StoredException != null )
142- {
143- ExceptionDispatchInfo . Capture ( exceptionHandler . StoredException ) . Throw ( ) ;
144- }
106+ /// <summary>
107+ /// Configures an instance of <typeparamref name="TApp" /> using <see cref="CommandLineApplication" /> to provide
108+ /// command line parsing on the given <paramref name="args" />.
109+ /// </summary>
110+ /// <typeparam name="TApp">The type of the command line application implementation</typeparam>
111+ /// <param name="hostBuilder">This instance</param>
112+ /// <param name="args">The command line arguments</param>
113+ /// <returns><see cref="IHostBuilder"/></returns>
114+ public static IHostBuilder UseCommandLineApplication < TApp > ( this IHostBuilder hostBuilder , string [ ] args )
115+ where TApp : class
116+ => UseCommandLineApplication < TApp > ( hostBuilder , args , _ => { } ) ;
145117
146- return state . ExitCode ;
147- }
148118
149119 /// <summary>
150120 /// Configures an instance of <typeparamref name="TApp" /> using <see cref="CommandLineApplication" /> to provide
@@ -158,15 +128,14 @@ public static async Task<int> RunCommandLineApplicationAsync(
158128 public static IHostBuilder UseCommandLineApplication < TApp > (
159129 this IHostBuilder hostBuilder ,
160130 string [ ] args ,
161- Action < CommandLineApplication < TApp > > configure = null )
131+ Action < CommandLineApplication < TApp > > configure )
162132 where TApp : class
163133 {
164134 configure ??= app => { } ;
165135 var state = new CommandLineState ( args ) ;
166136 hostBuilder . Properties [ typeof ( CommandLineState ) ] = state ;
167137 hostBuilder . ConfigureServices (
168- ( context , services )
169- =>
138+ ( context , services ) =>
170139 {
171140 services
172141 . TryAddSingleton < StoreExceptionHandler > ( ) ;
@@ -189,5 +158,23 @@ public static IHostBuilder UseCommandLineApplication<TApp>(
189158
190159 return hostBuilder ;
191160 }
161+
162+ private static void AddCommonServices ( this IServiceCollection services , CommandLineState state )
163+ {
164+ services
165+ . TryAddSingleton < StoreExceptionHandler > ( ) ;
166+ services
167+ . TryAddSingleton < IUnhandledExceptionHandler > ( provider => provider . GetRequiredService < StoreExceptionHandler > ( ) ) ;
168+ services
169+ . AddSingleton < IHostLifetime , CommandLineLifetime > ( )
170+ . TryAddSingleton ( PhysicalConsole . Singleton ) ;
171+ services
172+ . AddSingleton ( provider =>
173+ {
174+ state . SetConsole ( provider . GetService < IConsole > ( ) ) ;
175+ return state ;
176+ } )
177+ . AddSingleton < CommandLineContext > ( state ) ;
178+ }
192179 }
193180}
0 commit comments