@@ -95,11 +95,31 @@ public ICommand GetCommand(string name)
9595 return null ;
9696 }
9797
98+ /// <summary>
99+ /// Get command line command
100+ /// </summary>
101+ public ICommand GetCommandLineCommand ( )
102+ {
103+ var parser = CommandLineParser . NewFromEnvironment ( ) ;
104+ if ( parser . Count == 0 )
105+ {
106+ return null ;
107+ }
108+
109+ var argCommand = parser . Get ( 1 ) ;
110+ if ( string . IsNullOrWhiteSpace ( argCommand ) )
111+ {
112+ return null ;
113+ }
114+
115+ return GetCommand ( argCommand ) ;
116+ }
117+
98118 /// <summary>
99119 /// Execute command using the environment command line arguments
100120 /// </summary>
101121 /// <returns>Exit code</returns>
102- public async Task < int > ExecuteAsync ( PayrollHttpClient httpClient )
122+ public async Task < int > ExecuteAsync ( PayrollHttpClient httpClient = null )
103123 {
104124 var parser = CommandLineParser . NewFromEnvironment ( ) ;
105125 if ( parser . Count == 0 )
@@ -125,7 +145,7 @@ public async Task<int> ExecuteAsync(PayrollHttpClient httpClient)
125145 // command file
126146 if ( ! string . IsNullOrWhiteSpace ( argCommand ) && IsCommandFile ( argCommand ) )
127147 {
128- return await ExecuteFileAsync ( httpClient , parser ) ;
148+ return await ExecuteFileAsync ( parser , httpClient ) ;
129149 }
130150
131151 // single command using the environment command line parser
@@ -140,27 +160,23 @@ public async Task<int> ExecuteAsync(PayrollHttpClient httpClient)
140160 {
141161 return - 1 ;
142162 }
143- return await ExecuteAsync ( httpClient , command , parser ) ;
163+ return await ExecuteAsync ( command , parser , httpClient ) ;
144164 }
145165
146166 /// <summary>
147167 /// Execute specific command using the environment command line arguments
148168 /// </summary>
149169 /// <returns>Exit code</returns>
150- public async Task < int > ExecuteAsync ( PayrollHttpClient httpClient , ICommand command ) =>
151- await ExecuteAsync ( httpClient , command , CommandLineParser . NewFromEnvironment ( ) ) ;
170+ public async Task < int > ExecuteAsync ( ICommand command , PayrollHttpClient httpClient = null ) =>
171+ await ExecuteAsync ( command , CommandLineParser . NewFromEnvironment ( ) , httpClient ) ;
152172
153173 /// <summary>
154174 /// Execute specific command using a custom command line parser
155175 /// </summary>
156176 /// <returns>Exit code</returns>
157- private async Task < int > ExecuteAsync ( PayrollHttpClient httpClient ,
158- ICommand command , CommandLineParser commandLineParser )
177+ private async Task < int > ExecuteAsync ( ICommand command , CommandLineParser commandLineParser ,
178+ PayrollHttpClient httpClient = null )
159179 {
160- if ( httpClient == null )
161- {
162- throw new ArgumentNullException ( nameof ( httpClient ) ) ;
163- }
164180 if ( command == null )
165181 {
166182 throw new ArgumentNullException ( nameof ( command ) ) ;
@@ -199,9 +215,9 @@ private async Task<int> ExecuteAsync(PayrollHttpClient httpClient,
199215
200216 var context = new CommandContext (
201217 commandManager : this ,
202- httpClient : httpClient ,
203- logger : Logger ,
204218 console : Console ,
219+ logger : Logger ,
220+ httpClient : httpClient ,
205221 displayLevel : Console . DisplayLevel ) ;
206222 var result = await command . ExecuteAsync ( context , parameters ) ;
207223 Logger ? . Trace ( $ "{ command } result: { result } ") ;
@@ -238,12 +254,8 @@ public FileItem(string text, CommandLineParser parser, List<FileItem> children)
238254 internal List < FileItem > Children { get ; } = [ ] ;
239255 }
240256
241- private async Task < int > ExecuteFileAsync ( PayrollHttpClient httpClient , CommandLineParser parser )
257+ private async Task < int > ExecuteFileAsync ( CommandLineParser parser , PayrollHttpClient httpClient = null )
242258 {
243- if ( httpClient == null )
244- {
245- throw new ArgumentNullException ( nameof ( httpClient ) ) ;
246- }
247259 if ( parser == null )
248260 {
249261 throw new ArgumentNullException ( nameof ( parser ) ) ;
@@ -261,7 +273,7 @@ private async Task<int> ExecuteFileAsync(PayrollHttpClient httpClient, CommandLi
261273
262274 foreach ( var item in items )
263275 {
264- var exitCode = await ExecuteFileItemAsync ( httpClient , item ) ;
276+ var exitCode = await ExecuteFileItemAsync ( item , httpClient ) ;
265277 if ( exitCode != 0 )
266278 {
267279 return exitCode ;
@@ -274,12 +286,12 @@ private async Task<int> ExecuteFileAsync(PayrollHttpClient httpClient, CommandLi
274286 return 0 ;
275287 }
276288
277- private async Task < int > ExecuteFileItemAsync ( PayrollHttpClient httpClient , FileItem item )
289+ private async Task < int > ExecuteFileItemAsync ( FileItem item , PayrollHttpClient httpClient = null )
278290 {
279291 // command
280292 if ( item . Command != null )
281293 {
282- return await ExecuteAsync ( httpClient , item . Command , item . Parser ) ;
294+ return await ExecuteAsync ( item . Command , item . Parser , httpClient ) ;
283295 }
284296
285297 // command file
@@ -303,7 +315,7 @@ private async Task<int> ExecuteFileItemAsync(PayrollHttpClient httpClient, FileI
303315 // process children
304316 foreach ( var children in item . Children )
305317 {
306- var result = await ExecuteFileItemAsync ( httpClient , children ) ;
318+ var result = await ExecuteFileItemAsync ( children , httpClient ) ;
307319 if ( result != 0 )
308320 {
309321 return result ;
0 commit comments