@@ -113,6 +113,50 @@ public static async Task Can_bind_to_arguments_via_injection()
113
113
service . StringValue . Should ( ) . Be ( "TEST" ) ;
114
114
}
115
115
116
+ [ Fact ]
117
+ public static async Task Invokes_DerivedClass ( )
118
+ {
119
+ var service = new MyService ( ) ;
120
+
121
+ var cmd = new RootCommand ( ) ;
122
+ cmd . AddCommand ( new MyCommand ( ) ) ;
123
+ cmd . AddCommand ( new MyOtherCommand ( ) ) ;
124
+ var parser = new CommandLineBuilder ( cmd )
125
+ . UseHost ( ( builder ) => {
126
+ builder . ConfigureServices ( services =>
127
+ {
128
+ services . AddTransient ( x => service ) ;
129
+ } )
130
+ . UseCommandHandler < MyCommand , MyCommand . MyDerivedHandler > ( )
131
+ . UseCommandHandler < MyOtherCommand , MyOtherCommand . MyDerivedHandler > ( ) ;
132
+ } )
133
+ . Build ( ) ;
134
+
135
+ await parser . InvokeAsync ( new string [ ] { "mycommand" , "--int-option" , "54" } ) ;
136
+ service . Value . Should ( ) . Be ( 54 ) ;
137
+
138
+ await parser . InvokeAsync ( new string [ ] { "myothercommand" , "TEST" } ) ;
139
+ service . StringValue . Should ( ) . Be ( "TEST" ) ;
140
+ }
141
+
142
+ public abstract class MyBaseHandler : ICommandHandler
143
+ {
144
+ public int IntOption { get ; set ; } // bound from option
145
+ public IConsole Console { get ; set ; } // bound from DI
146
+
147
+ public int Invoke ( InvocationContext context )
148
+ {
149
+ return Act ( ) ;
150
+ }
151
+
152
+ public Task < int > InvokeAsync ( InvocationContext context )
153
+ {
154
+ return Task . FromResult ( Act ( ) ) ;
155
+ }
156
+
157
+ protected abstract int Act ( ) ;
158
+ }
159
+
116
160
public class MyCommand : Command
117
161
{
118
162
public MyCommand ( ) : base ( name : "mycommand" )
@@ -144,6 +188,22 @@ public Task<int> InvokeAsync(InvocationContext context)
144
188
return Task . FromResult ( IntOption ) ;
145
189
}
146
190
}
191
+
192
+ public class MyDerivedHandler : MyBaseHandler
193
+ {
194
+ private readonly MyService service ;
195
+
196
+ public MyDerivedHandler ( MyService service )
197
+ {
198
+ this . service = service ;
199
+ }
200
+
201
+ protected override int Act ( )
202
+ {
203
+ service . Value = IntOption ;
204
+ return IntOption ;
205
+ }
206
+ }
147
207
}
148
208
149
209
public class MyOtherCommand : Command
@@ -177,6 +237,25 @@ public Task<int> InvokeAsync(InvocationContext context)
177
237
return Task . FromResult ( service . Action ? . Invoke ( ) ?? 0 ) ;
178
238
}
179
239
}
240
+
241
+ public class MyDerivedHandler : MyBaseHandler
242
+ {
243
+ private readonly MyService service ;
244
+
245
+ public MyDerivedHandler ( MyService service )
246
+ {
247
+ this . service = service ;
248
+ }
249
+
250
+ public string One { get ; set ; }
251
+
252
+ protected override int Act ( )
253
+ {
254
+ service . Value = IntOption ;
255
+ service . StringValue = One ;
256
+ return service . Action ? . Invoke ( ) ?? 0 ;
257
+ }
258
+ }
180
259
}
181
260
182
261
public class MyService
0 commit comments