22using System ;
33using System . Data . Common ;
44using System . Diagnostics . CodeAnalysis ;
5+ using System . Reflection ;
56using System . Reflection . Metadata ;
67using System . Text ;
78
@@ -168,9 +169,10 @@ public record class CommandParameter
168169 public required string Description { get ; init ; }
169170 public bool RequireCheckArgumentParsed => ! ( HasDefaultValue || IsParams || IsFlag ) ;
170171
172+ // increment = false when passed from [Argument]
171173 public string BuildParseMethod ( int argCount , string argumentName , WellKnownTypes wellKnownTypes , bool increment )
172174 {
173- var index = increment ? "++i " : "i " ;
175+ var incrementIndex = increment ? "!TryIncrementIndex(ref i, args.Length) || " : "" ;
174176 return Core ( Type , false ) ;
175177
176178 string Core ( ITypeSymbol type , bool nullable )
@@ -190,13 +192,22 @@ string Core(ITypeSymbol type, bool nullable)
190192
191193 if ( CustomParserType != null )
192194 {
193- return $ "if (!{ CustomParserType . ToFullyQualifiedFormatDisplayString ( ) } .TryParse(args[{ index } ], { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
195+ return $ "if ({ incrementIndex } !{ CustomParserType . ToFullyQualifiedFormatDisplayString ( ) } .TryParse(args[i ], { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
194196 }
195197
196198 switch ( type . SpecialType )
197199 {
198200 case SpecialType . System_String :
199- return $ "arg{ argCount } = args[{ index } ];"; // no parse
201+ // no parse
202+ if ( increment )
203+ {
204+ return $ "if (!TryIncrementIndex(ref i, args.Length)) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }} else {{ arg{ argCount } = args[i]; }}";
205+ }
206+ else
207+ {
208+ return $ "arg{ argCount } = args[i];";
209+ }
210+
200211 case SpecialType . System_Boolean :
201212 return $ "arg{ argCount } = true;"; // bool is true flag
202213 case SpecialType . System_Char :
@@ -218,7 +229,7 @@ string Core(ITypeSymbol type, bool nullable)
218229 // Enum
219230 if ( type . TypeKind == TypeKind . Enum )
220231 {
221- return $ "if (!Enum.TryParse<{ type . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) } >(args[{ index } ], true, { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
232+ return $ "if ({ incrementIndex } !Enum.TryParse<{ type . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) } >(args[i ], true, { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
222233 }
223234
224235 // ParamsArray
@@ -236,7 +247,7 @@ string Core(ITypeSymbol type, bool nullable)
236247 {
237248 if ( elementType . AllInterfaces . Any ( x => x . EqualsUnconstructedGenericType ( parsable ) ) )
238249 {
239- return $ "if (!TrySplitParse(args[{ index } ], { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
250+ return $ "if ({ incrementIndex } !TrySplitParse(args[i ], { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
240251 }
241252 }
242253 break ;
@@ -260,15 +271,15 @@ string Core(ITypeSymbol type, bool nullable)
260271
261272 if ( tryParseKnownPrimitive )
262273 {
263- return $ "if (!{ type . ToFullyQualifiedFormatDisplayString ( ) } .TryParse(args[{ index } ], { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
274+ return $ "if ({ incrementIndex } !{ type . ToFullyQualifiedFormatDisplayString ( ) } .TryParse(args[i ], { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
264275 }
265276 else if ( tryParseIParsable )
266277 {
267- return $ "if (!{ type . ToFullyQualifiedFormatDisplayString ( ) } .TryParse(args[{ index } ], null, { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
278+ return $ "if ({ incrementIndex } !{ type . ToFullyQualifiedFormatDisplayString ( ) } .TryParse(args[i ], null, { outArgVar } )) {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}{ elseExpr } ";
268279 }
269280 else
270281 {
271- return $ "try {{ arg{ argCount } = System.Text.Json.JsonSerializer.Deserialize<{ type . ToFullyQualifiedFormatDisplayString ( ) } >(args[{ index } ]); }} catch {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}";
282+ return $ "try {{ arg{ argCount } = System.Text.Json.JsonSerializer.Deserialize<{ type . ToFullyQualifiedFormatDisplayString ( ) } >(args[{ ( increment ? "++i" : "i" ) } ]); }} catch {{ ThrowArgumentParseFailed(\" { argumentName } \" , args[i]); }}";
272283 }
273284 }
274285 }
0 commit comments