2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
- using System . Runtime . ExceptionServices ;
6
5
using System . Threading ;
7
6
using System . Threading . Tasks ;
8
7
using McMaster . Extensions . CommandLineUtils ;
@@ -36,7 +35,7 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
36
35
CancellationToken cancellationToken = default )
37
36
where TApp : class
38
37
{
39
- return await RunCommandLineApplicationAsync < TApp > ( hostBuilder , args , null , cancellationToken ) ;
38
+ return await RunCommandLineApplicationAsync < TApp > ( hostBuilder , args , app => { } , cancellationToken ) ;
40
39
}
41
40
42
41
/// <summary>
@@ -57,40 +56,19 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
57
56
CancellationToken cancellationToken = default )
58
57
where TApp : class
59
58
{
60
- configure ??= app => { } ;
61
- var exceptionHandler = new StoreExceptionHandler ( ) ;
62
59
var state = new CommandLineState ( args ) ;
63
60
hostBuilder . Properties [ typeof ( CommandLineState ) ] = state ;
64
61
hostBuilder . ConfigureServices (
65
62
( context , services )
66
63
=>
67
64
{
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 ) ;
83
68
} ) ;
84
69
85
70
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 ) ;
94
72
}
95
73
96
74
/// <summary>
@@ -110,41 +88,33 @@ public static async Task<int> RunCommandLineApplicationAsync(
110
88
Action < CommandLineApplication > configure ,
111
89
CancellationToken cancellationToken = default )
112
90
{
113
- var exceptionHandler = new StoreExceptionHandler ( ) ;
114
91
var state = new CommandLineState ( args ) ;
115
92
hostBuilder . Properties [ typeof ( CommandLineState ) ] = state ;
116
93
hostBuilder . ConfigureServices (
117
94
( context , services )
118
95
=>
119
96
{
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 ) ;
135
100
} ) ;
136
101
137
102
using var host = hostBuilder . Build ( ) ;
103
+ return await host . RunCommandLineApplicationAsync ( cancellationToken ) ;
104
+ }
138
105
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 , _ => { } ) ;
145
117
146
- return state . ExitCode ;
147
- }
148
118
149
119
/// <summary>
150
120
/// Configures an instance of <typeparamref name="TApp" /> using <see cref="CommandLineApplication" /> to provide
@@ -158,15 +128,14 @@ public static async Task<int> RunCommandLineApplicationAsync(
158
128
public static IHostBuilder UseCommandLineApplication < TApp > (
159
129
this IHostBuilder hostBuilder ,
160
130
string [ ] args ,
161
- Action < CommandLineApplication < TApp > > configure = null )
131
+ Action < CommandLineApplication < TApp > > configure )
162
132
where TApp : class
163
133
{
164
134
configure ??= app => { } ;
165
135
var state = new CommandLineState ( args ) ;
166
136
hostBuilder . Properties [ typeof ( CommandLineState ) ] = state ;
167
137
hostBuilder . ConfigureServices (
168
- ( context , services )
169
- =>
138
+ ( context , services ) =>
170
139
{
171
140
services
172
141
. TryAddSingleton < StoreExceptionHandler > ( ) ;
@@ -189,5 +158,23 @@ public static IHostBuilder UseCommandLineApplication<TApp>(
189
158
190
159
return hostBuilder ;
191
160
}
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
+ }
192
179
}
193
180
}
0 commit comments