1
1
using System ;
2
- using System . Collections . Generic ;
3
2
using MGR . CommandLineParser . Command ;
4
3
using MGR . CommandLineParser . Diagnostics ;
5
4
using MGR . CommandLineParser . Extensibility ;
@@ -20,13 +19,13 @@ internal ParserEngine(IServiceProvider serviceProvider, ILoggerFactory loggerFac
20
19
_logger = loggerFactory . CreateLogger < LoggerCategory . Parser > ( ) ;
21
20
}
22
21
23
- internal ParsingResult Parse < TCommand > ( IEnumerator < string > argumentsEnumerator ) where TCommand : class , ICommand
22
+ internal ParsingResult Parse < TCommand > ( Arguments arguments ) where TCommand : class , ICommand
24
23
{
25
24
using ( _logger . BeginParsingForSpecificCommandType ( typeof ( TCommand ) ) )
26
25
{
27
26
var commandTypeProviders = _serviceProvider . GetServices < ICommandTypeProvider > ( ) ;
28
27
var commandType = commandTypeProviders . GetCommandType < TCommand > ( ) ;
29
- var parsingResult = ParseImpl ( argumentsEnumerator , commandType ) ;
28
+ var parsingResult = ParseImpl ( arguments , commandType ) ;
30
29
if ( parsingResult . ParsingResultCode == CommandParsingResultCode . NoCommandFound )
31
30
{
32
31
_logger . NoCommandFoundAfterSpecificParsing ( ) ;
@@ -41,46 +40,47 @@ internal ParsingResult Parse<TCommand>(IEnumerator<string> argumentsEnumerator)
41
40
}
42
41
}
43
42
44
- internal ParsingResult ParseWithDefaultCommand < TCommand > ( IEnumerator < string > argumentsEnumerator )
43
+ internal ParsingResult ParseWithDefaultCommand < TCommand > ( Arguments arguments )
45
44
where TCommand : class , ICommand
46
45
{
47
46
using ( _logger . BeginParsingWithDefaultCommandType ( typeof ( TCommand ) ) )
48
47
{
49
- var commandName = argumentsEnumerator . GetNextCommandLineItem ( ) ;
50
- if ( commandName != null )
48
+ if ( arguments . Advance ( ) )
51
49
{
50
+ var commandName = arguments . GetCurrent ( ) ;
52
51
_logger . ArgumentProvidedWithDefaultCommandType ( commandName ) ;
53
52
var commandTypeProviders = _serviceProvider . GetServices < ICommandTypeProvider > ( ) ;
54
53
var commandType = commandTypeProviders . GetCommandType ( commandName ) ;
55
54
if ( commandType != null )
56
55
{
57
56
_logger . CommandTypeFoundWithDefaultCommandType ( commandName ) ;
58
- return ParseImpl ( argumentsEnumerator , commandType ) ;
57
+ return ParseImpl ( arguments , commandType ) ;
59
58
}
60
59
61
60
_logger . NoCommandTypeFoundWithDefaultCommandType ( commandName , typeof ( TCommand ) ) ;
62
- var withArgumentsCommandResult = Parse < TCommand > ( argumentsEnumerator . PrefixWith ( commandName ) ) ;
61
+ arguments . Revert ( ) ;
62
+ var withArgumentsCommandResult = Parse < TCommand > ( arguments ) ;
63
63
return withArgumentsCommandResult ;
64
64
65
65
}
66
66
67
67
_logger . NoArgumentProvidedWithDefaultCommandType ( typeof ( TCommand ) ) ;
68
- var noArgumentsCommandResult = Parse < TCommand > ( argumentsEnumerator ) ;
68
+ var noArgumentsCommandResult = Parse < TCommand > ( arguments ) ;
69
69
return noArgumentsCommandResult ;
70
70
}
71
71
}
72
72
73
- internal ParsingResult Parse ( IEnumerator < string > argumentsEnumerator )
73
+ internal ParsingResult Parse ( Arguments arguments )
74
74
{
75
75
_logger . ParseForNotAlreadyKnownCommand ( ) ;
76
- var commandName = argumentsEnumerator . GetNextCommandLineItem ( ) ;
77
- if ( commandName == null )
76
+ if ( ! arguments . Advance ( ) )
78
77
{
79
78
_logger . NoCommandNameForNotAlreadyKnownCommand ( ) ;
80
79
var helpWriter = _serviceProvider . GetRequiredService < IHelpWriter > ( ) ;
81
80
helpWriter . WriteCommandListing ( ) ;
82
81
return new ParsingResult ( null , null , CommandParsingResultCode . NoCommandNameProvided ) ;
83
82
}
83
+ var commandName = arguments . GetCurrent ( ) ;
84
84
85
85
using ( _logger . BeginParsingUsingCommandName ( commandName ) )
86
86
{
@@ -95,14 +95,14 @@ internal ParsingResult Parse(IEnumerator<string> argumentsEnumerator)
95
95
}
96
96
97
97
_logger . CommandTypeFoundForNotAlreadyKnownCommand ( commandName ) ;
98
- return ParseImpl ( argumentsEnumerator , commandType ) ;
98
+ return ParseImpl ( arguments , commandType ) ;
99
99
}
100
100
101
101
}
102
102
103
- private ParsingResult ParseImpl ( IEnumerator < string > argumentsEnumerator , ICommandType commandType )
103
+ private ParsingResult ParseImpl ( Arguments arguments , ICommandType commandType )
104
104
{
105
- var commandObjectBuilder = ExtractCommandLineOptions ( commandType , argumentsEnumerator ) ;
105
+ var commandObjectBuilder = ExtractCommandLineOptions ( commandType , arguments ) ;
106
106
if ( commandObjectBuilder == null )
107
107
{
108
108
return new ParsingResult ( null , null , CommandParsingResultCode . CommandParametersNotValid ) ;
@@ -117,17 +117,14 @@ private ParsingResult ParseImpl(IEnumerator<string> argumentsEnumerator, IComman
117
117
}
118
118
return new ParsingResult ( commandObjectBuilder . GenerateCommandObject ( ) , null , CommandParsingResultCode . Success ) ;
119
119
}
120
- private ICommandObjectBuilder ExtractCommandLineOptions ( ICommandType commandType , IEnumerator < string > argumentsEnumerator )
120
+
121
+ private ICommandObjectBuilder ExtractCommandLineOptions ( ICommandType commandType , Arguments arguments )
121
122
{
122
123
var commandObjectBuilder = commandType . CreateCommandObjectBuilder ( _serviceProvider ) ;
123
124
var alwaysPutInArgumentList = false ;
124
- while ( true )
125
+ while ( arguments . Advance ( ) )
125
126
{
126
- var argument = argumentsEnumerator . GetNextCommandLineItem ( ) ;
127
- if ( argument == null )
128
- {
129
- break ;
130
- }
127
+ var argument = arguments . GetCurrent ( ) ;
131
128
if ( argument . Equals ( Constants . EndOfOptions ) )
132
129
{
133
130
alwaysPutInArgumentList = true ;
@@ -166,7 +163,17 @@ private ICommandObjectBuilder ExtractCommandLineOptions(ICommandType commandType
166
163
167
164
if ( option . ShouldProvideValue )
168
165
{
169
- value = value ?? argumentsEnumerator . GetNextCommandLineItem ( ) ;
166
+ if ( value == null )
167
+ {
168
+ if ( ! arguments . Advance ( ) )
169
+ {
170
+ var console = _serviceProvider . GetRequiredService < IConsole > ( ) ;
171
+ console . WriteLineError ( Constants . ExceptionMessages . FormatParserOptionValueNotFoundForCommand ( commandType . Metadata . Name , optionText ) ) ;
172
+ return null ;
173
+ }
174
+
175
+ value = arguments . GetCurrent ( ) ;
176
+ }
170
177
}
171
178
172
179
option . AssignValue ( value ) ;
0 commit comments