Commit 4eaa06d
Fix parameter index not incrementing in nested subcommands (#32)
## Fix for Nested Subcommand Parameter Bug ✅ COMPLETE
- [x] Identified the bug: `_paramIndex` is not incremented when
parameters are matched
- [x] Determine the best place to increment `_paramIndex`
- [x] Implement the fix to increment `_paramIndex` after reading a
parameter value
- [x] Run the failing test to verify the fix works
- [x] Run all tests to ensure no regressions
- [x] Review and commit the changes
- [x] Code review - no issues found
- [x] Security scan - no vulnerabilities found
- [x] Manual verification - confirmed fix works
- [x] Refactored to remove side effects from CheckFields method
- [x] Replaced tuple booleans with FieldKind enum for better clarity
## Summary
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 `_paramIndex` field in `Deserializer.cs` was never incremented when
a parameter was matched in the `CheckFields` method. This caused all
parameters to match against ordinal 0, resulting in duplicate field
assignments.
### Changes Made
1. **Test File** (`test/Serde.CmdLine.Test/SubCommandTests.cs`):
- Added `NestedSubCommandWithTwoParameters` test case
- Creates a `copy` subcommand with two parameters: `source` and
`destination`
- Tests parsing: `["copy", "source.txt", "dest.txt"]`
2. **Bug Fix** (`src/Serde.CmdLine/Deserializer.DeserializeType.cs`):
- Modified `CheckFields` signature to return `(int fieldIndex, FieldKind
fieldKind)`
- Returns `FieldKind` enum indicating the type of field matched
- Caller uses switch statement to increment appropriate index based on
field kind
- `CheckFields` is side-effect free, following established patterns
3. **New Type** (`src/Serde.CmdLine/OptionTypes.cs`):
- Added `FieldKind` enum with values: None, Option, SubCommand,
CommandGroup, Parameter
- Each value is documented with its behavior regarding index increments
- Provides clearer semantic meaning than tuple of booleans
### Verification
✅ **Unit Tests**
- New test `NestedSubCommandWithTwoParameters` passes
- All 21 CmdLine tests pass
- All 4 Analyzer tests pass
✅ **Code Design**
- `CheckFields` is side-effect free
- Uses enum instead of tuple booleans for better readability and
maintainability
- Switch statement clearly maps field kinds to their required index
increments
- Follows the established pattern where method returns signals and
caller handles state mutations
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/serdedotnet/serde.cmdline/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>1 parent 90fd272 commit 4eaa06d
File tree
3 files changed
+83
-9
lines changed- src/Serde.CmdLine
- test/Serde.CmdLine.Test
3 files changed
+83
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
| 62 | + | |
62 | 63 | | |
63 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
64 | 71 | | |
65 | 72 | | |
66 | 73 | | |
| |||
156 | 163 | | |
157 | 164 | | |
158 | 165 | | |
159 | | - | |
| 166 | + | |
160 | 167 | | |
161 | 168 | | |
162 | 169 | | |
163 | 170 | | |
164 | 171 | | |
165 | | - | |
| 172 | + | |
166 | 173 | | |
167 | 174 | | |
168 | 175 | | |
169 | 176 | | |
170 | 177 | | |
171 | 178 | | |
172 | 179 | | |
173 | | - | |
| 180 | + | |
174 | 181 | | |
175 | 182 | | |
176 | 183 | | |
| |||
180 | 187 | | |
181 | 188 | | |
182 | 189 | | |
183 | | - | |
| 190 | + | |
184 | 191 | | |
185 | 192 | | |
186 | 193 | | |
| |||
193 | 200 | | |
194 | 201 | | |
195 | 202 | | |
196 | | - | |
| 203 | + | |
197 | 204 | | |
198 | 205 | | |
199 | | - | |
| 206 | + | |
200 | 207 | | |
201 | 208 | | |
202 | 209 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
6 | 30 | | |
7 | 31 | | |
8 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
194 | 237 | | |
0 commit comments