Split mflags.go into smaller, focused files#13
Conversation
mflags.go had grown to ~2300 lines covering four distinct concerns. Pulled each one into its own file so future work (more value types, richer help formatting, new struct tags) lands somewhere obvious: mflags.go (823) core types, registration, parsing values.go (806) all xxxValue/xxxPtrValue implementations fromstruct.go (580) FromStruct and struct tag reflection help.go (146) ShowHelp, WriteFlagHelp, formatFlagLine Pure file reorganization, no behavior changes. Also removed some stray json/xml tags from an unexported test struct field that was tripping go vet. Closes MIR-988
📝 WalkthroughWalkthroughThis pull request reorganizes the mflags package by refactoring code into separate files. A new Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
help.go (1)
87-146: Consider extracting duplicate max-position calculation logic.The pattern of finding
maxPosby iteratingposFieldsand then iterating0..maxPosappears twice inShowHelp()(lines 91-104 and lines 113-124). This is a minor duplication.♻️ Optional: Extract helper method
// getOrderedPositionalFields returns positional fields sorted by position. func (f *FlagSet) getOrderedPositionalFields() []*PositionalField { if len(f.posFields) == 0 { return nil } maxPos := -1 for pos := range f.posFields { if pos > maxPos { maxPos = pos } } result := make([]*PositionalField, 0, maxPos+1) for i := 0; i <= maxPos; i++ { if field, ok := f.posFields[i]; ok { result = append(result, field) } } return result }Note:
GetPositionalFields()inmflags.goalready provides similar functionality and could potentially be reused here.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@help.go` around lines 87 - 146, ShowHelp currently duplicates the logic that computes maxPos and iterates 0..maxPos over f.posFields; extract that logic into a helper and reuse it to avoid duplication. Create or call a helper that returns ordered positional fields (e.g., getOrderedPositionalFields or reuse existing GetPositionalFields in mflags.go) and replace the two blocks in ShowHelp that iterate f.posFields/maxPos with a single loop over the helper's result; keep handling of f.restField and usage/env formatting unchanged and ensure the helper returns nil/empty when no positional fields exist.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@help.go`:
- Around line 87-146: ShowHelp currently duplicates the logic that computes
maxPos and iterates 0..maxPos over f.posFields; extract that logic into a helper
and reuse it to avoid duplication. Create or call a helper that returns ordered
positional fields (e.g., getOrderedPositionalFields or reuse existing
GetPositionalFields in mflags.go) and replace the two blocks in ShowHelp that
iterate f.posFields/maxPos with a single loop over the helper's result; keep
handling of f.restField and usage/env formatting unchanged and ensure the helper
returns nil/empty when no positional fields exist.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8bf0a1e8-ae69-4638-9d88-8ec1a9726eb8
📒 Files selected for processing (5)
fromstruct.gohelp.gomflags.gomflags_test.govalues.go
mflags.gohad grown to ~2300 lines and was getting harder to navigate. It covered four pretty distinct concerns that don't really need to live together: core types and parsing, value type implementations, struct tag reflection, and help rendering.Broke it into four files along those natural seams. Each file is now in the 150-800 line range, and when you need to add a new value type or tweak help output, you know exactly where to go. No behavior changes, just moving code around within the same package.
Also cleaned up a
go vetwarning from a test struct that hadjson/xmltags on an unexported field.Closes MIR-988