Skip to content

Split mflags.go into smaller, focused files#13

Merged
phinze merged 1 commit into
mainfrom
phinze/mir-988-split-mflagsgo-into-smaller-focused-files
Apr 9, 2026
Merged

Split mflags.go into smaller, focused files#13
phinze merged 1 commit into
mainfrom
phinze/mir-988-split-mflagsgo-into-smaller-focused-files

Conversation

@phinze

@phinze phinze commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

mflags.go had 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 vet warning from a test struct that had json/xml tags on an unexported field.

Closes MIR-988

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
@phinze phinze requested a review from a team as a code owner April 8, 2026 21:43
@coderabbitai

coderabbitai Bot commented Apr 8, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This pull request reorganizes the mflags package by refactoring code into separate files. A new fromstruct.go file implements reflection-based flag and positional argument definition from struct fields, including FromStruct and ParseStruct functions with struct tag parsing and validation. A new help.go file provides help output formatting via WriteFlagHelp and ShowHelp methods. A new values.go file contains flag value type implementations for scalars, arrays, pointers, and choice-constrained strings. The main mflags.go file removes these components, and a test file is updated to remove tags from an unexported field.


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
help.go (1)

87-146: Consider extracting duplicate max-position calculation logic.

The pattern of finding maxPos by iterating posFields and then iterating 0..maxPos appears twice in ShowHelp() (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() in mflags.go already 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

📥 Commits

Reviewing files that changed from the base of the PR and between 1b813c2 and d48e5da.

📒 Files selected for processing (5)
  • fromstruct.go
  • help.go
  • mflags.go
  • mflags_test.go
  • values.go

@phinze phinze merged commit 9b4f3df into main Apr 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants