Fix parameter index not incrementing in nested subcommands#32
Conversation
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
| // Parameters are positional, so we check the current param index | ||
| if (_deserializer._paramIndex == param.Ordinal) | ||
| { | ||
| _deserializer._paramIndex++; |
There was a problem hiding this comment.
@copilot is the rest of this method non-side effecting? Do we want to move the increment out of the method and somehow add signaling that we matched a parameter?
There was a problem hiding this comment.
Good catch! I've refactored CheckFields to be side-effect free. It now returns (int fieldIndex, bool incArgs, bool isParameter) where isParameter signals when a parameter is matched. The caller increments _paramIndex based on this signal (lines 65-68), following the same pattern as incArgs for argIndex.
Commit: fd20886
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
| } | ||
|
|
||
| var (fieldIndex, incArgs) = CheckFields(arg); | ||
| var (fieldIndex, incArgs, isParameter) = CheckFields(arg); |
There was a problem hiding this comment.
Is the additional parameter to this tuple getting a bit messy? The method basically wants to return an index, then an indication of what type of field was returned, so we can decide whether or not to increment args, parameter index, etc. Would an enum value be better suited for this? @copilot
There was a problem hiding this comment.
You're right, an enum is much clearer. I've refactored to use a FieldKind enum with values: None, Option, SubCommand, CommandGroup, and Parameter. The method now returns (int fieldIndex, FieldKind fieldKind) and the caller uses a switch statement to determine which index to increment. This is much more readable and maintainable than the tuple of booleans.
Commit: 215bbe9
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Fix for Nested Subcommand Parameter Bug ✅ COMPLETE
_paramIndexis not incremented when parameters are matched_paramIndex_paramIndexafter reading a parameter valueSummary
Successfully fixed the bug where multiple command parameters in nested subcommands failed to parse correctly. The second parameter was being treated as a duplicate of the first, causing a "Duplicate key" error.
Root Cause
The
_paramIndexfield inDeserializer.cswas never incremented when a parameter was matched in theCheckFieldsmethod. This caused all parameters to match against ordinal 0, resulting in duplicate field assignments.Changes Made
Test File (
test/Serde.CmdLine.Test/SubCommandTests.cs):NestedSubCommandWithTwoParameterstest casecopysubcommand with two parameters:sourceanddestination["copy", "source.txt", "dest.txt"]Bug Fix (
src/Serde.CmdLine/Deserializer.DeserializeType.cs):CheckFieldssignature to return(int fieldIndex, FieldKind fieldKind)FieldKindenum indicating the type of field matchedCheckFieldsis side-effect free, following established patternsNew Type (
src/Serde.CmdLine/OptionTypes.cs):FieldKindenum with values: None, Option, SubCommand, CommandGroup, ParameterVerification
✅ Unit Tests
NestedSubCommandWithTwoParameterspasses✅ Code Design
CheckFieldsis side-effect free✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.