@@ -21,13 +21,13 @@ internal ParserEngine(IServiceProvider serviceProvider, ILoggerFactory loggerFac
2121 _logger = loggerFactory . CreateLogger < LoggerCategory . Parser > ( ) ;
2222 }
2323
24- internal async Task < ParsingResult > Parse < TCommand > ( IEnumerator < string > argumentsEnumerator ) where TCommand : class , ICommand
24+ internal async Task < ParsingResult > Parse < TCommand > ( Arguments arguments ) where TCommand : class , ICommand
2525 {
2626 using ( _logger . BeginParsingForSpecificCommandType ( typeof ( TCommand ) ) )
2727 {
2828 var commandTypeProviders = _serviceProvider . GetServices < ICommandTypeProvider > ( ) ;
2929 var commandType = await commandTypeProviders . GetCommandType < TCommand > ( ) ;
30- var parsingResult = ParseImpl ( argumentsEnumerator , commandType ) ;
30+ var parsingResult = ParseImpl ( arguments , commandType ) ;
3131 if ( parsingResult . ParsingResultCode == CommandParsingResultCode . NoCommandFound )
3232 {
3333 _logger . NoCommandFoundAfterSpecificParsing ( ) ;
@@ -42,46 +42,47 @@ internal async Task<ParsingResult> Parse<TCommand>(IEnumerator<string> arguments
4242 }
4343 }
4444
45- internal async Task < ParsingResult > ParseWithDefaultCommand < TCommand > ( IEnumerator < string > argumentsEnumerator )
45+ internal async Task < ParsingResult > ParseWithDefaultCommand < TCommand > ( Arguments arguments )
4646 where TCommand : class , ICommand
4747 {
4848 using ( _logger . BeginParsingWithDefaultCommandType ( typeof ( TCommand ) ) )
4949 {
50- var commandName = argumentsEnumerator . GetNextCommandLineItem ( ) ;
51- if ( commandName != null )
50+ if ( arguments . Advance ( ) )
5251 {
52+ var commandName = arguments . GetCurrent ( ) ;
5353 _logger . ArgumentProvidedWithDefaultCommandType ( commandName ) ;
5454 var commandTypeProviders = _serviceProvider . GetServices < ICommandTypeProvider > ( ) ;
5555 var commandType = await commandTypeProviders . GetCommandType ( commandName ) ;
5656 if ( commandType != null )
5757 {
5858 _logger . CommandTypeFoundWithDefaultCommandType ( commandName ) ;
59- return ParseImpl ( argumentsEnumerator , commandType ) ;
59+ return ParseImpl ( arguments , commandType ) ;
6060 }
6161
6262 _logger . NoCommandTypeFoundWithDefaultCommandType ( commandName , typeof ( TCommand ) ) ;
63- var withArgumentsCommandResult = await Parse < TCommand > ( argumentsEnumerator . PrefixWith ( commandName ) ) ;
63+ arguments . Revert ( ) ;
64+ var withArgumentsCommandResult = await Parse < TCommand > ( arguments ) ;
6465 return withArgumentsCommandResult ;
6566
6667 }
6768
6869 _logger . NoArgumentProvidedWithDefaultCommandType ( typeof ( TCommand ) ) ;
69- var noArgumentsCommandResult = await Parse < TCommand > ( argumentsEnumerator ) ;
70+ var noArgumentsCommandResult = await Parse < TCommand > ( arguments ) ;
7071 return noArgumentsCommandResult ;
7172 }
7273 }
7374
74- internal async Task < ParsingResult > Parse ( IEnumerator < string > argumentsEnumerator )
75+ internal async Task < ParsingResult > Parse ( Arguments arguments )
7576 {
7677 _logger . ParseForNotAlreadyKnownCommand ( ) ;
77- var commandName = argumentsEnumerator . GetNextCommandLineItem ( ) ;
78- if ( commandName == null )
78+ if ( ! arguments . Advance ( ) )
7979 {
8080 _logger . NoCommandNameForNotAlreadyKnownCommand ( ) ;
8181 var helpWriter = _serviceProvider . GetRequiredService < IHelpWriter > ( ) ;
8282 await helpWriter . WriteCommandListing ( ) ;
8383 return new ParsingResult ( null , null , CommandParsingResultCode . NoCommandNameProvided ) ;
8484 }
85+ var commandName = arguments . GetCurrent ( ) ;
8586
8687 using ( _logger . BeginParsingUsingCommandName ( commandName ) )
8788 {
@@ -96,14 +97,14 @@ internal async Task<ParsingResult> Parse(IEnumerator<string> argumentsEnumerator
9697 }
9798
9899 _logger . CommandTypeFoundForNotAlreadyKnownCommand ( commandName ) ;
99- return ParseImpl ( argumentsEnumerator , commandType ) ;
100+ return ParseImpl ( arguments , commandType ) ;
100101 }
101102
102103 }
103104
104- private ParsingResult ParseImpl ( IEnumerator < string > argumentsEnumerator , ICommandType commandType )
105+ private ParsingResult ParseImpl ( Arguments arguments , ICommandType commandType )
105106 {
106- var commandObjectBuilder = ExtractCommandLineOptions ( commandType , argumentsEnumerator ) ;
107+ var commandObjectBuilder = ExtractCommandLineOptions ( commandType , arguments ) ;
107108 if ( commandObjectBuilder == null )
108109 {
109110 return new ParsingResult ( null , null , CommandParsingResultCode . CommandParametersNotValid ) ;
@@ -118,21 +119,18 @@ private ParsingResult ParseImpl(IEnumerator<string> argumentsEnumerator, IComman
118119 }
119120 return new ParsingResult ( commandObjectBuilder . GenerateCommandObject ( ) , null , CommandParsingResultCode . Success ) ;
120121 }
121- private ICommandObjectBuilder ExtractCommandLineOptions ( ICommandType commandType , IEnumerator < string > argumentsEnumerator )
122+
123+ private ICommandObjectBuilder ExtractCommandLineOptions ( ICommandType commandType , Arguments arguments )
122124 {
123125 var commandObjectBuilder = commandType . CreateCommandObjectBuilder ( _serviceProvider ) ;
124126 if ( commandObjectBuilder == null )
125127 {
126128 return null ;
127129 }
128130 var alwaysPutInArgumentList = false ;
129- while ( true )
131+ while ( arguments . Advance ( ) )
130132 {
131- var argument = argumentsEnumerator . GetNextCommandLineItem ( ) ;
132- if ( argument == null )
133- {
134- break ;
135- }
133+ var argument = arguments . GetCurrent ( ) ;
136134 if ( argument . Equals ( Constants . EndOfOptions ) )
137135 {
138136 alwaysPutInArgumentList = true ;
@@ -171,7 +169,17 @@ private ICommandObjectBuilder ExtractCommandLineOptions(ICommandType commandType
171169
172170 if ( option . ShouldProvideValue )
173171 {
174- value = value ?? argumentsEnumerator . GetNextCommandLineItem ( ) ;
172+ if ( value == null )
173+ {
174+ if ( ! arguments . Advance ( ) )
175+ {
176+ var console = _serviceProvider . GetRequiredService < IConsole > ( ) ;
177+ console . WriteLineError ( Constants . ExceptionMessages . FormatParserOptionValueNotFoundForCommand ( commandType . Metadata . Name , optionText ) ) ;
178+ return null ;
179+ }
180+
181+ value = arguments . GetCurrent ( ) ;
182+ }
175183 }
176184
177185 option . AssignValue ( value ) ;
0 commit comments