fix: reject duplicate flag names - #2396
Conversation
Detect duplicate local flag names and aliases after built-in defaults are added so ambiguous command configurations fail instead of rendering repeated flags or shadowing built-in help. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1c0774c to
44743e2
Compare
Review: reject duplicate flag namesVerified the change against the base. Core behavior works, but there is one behavioral regression worth addressing before merge. 1. Moderate — a user-defined
|
What type of PR is this?
What this PR does / why we need it:
helporh, the auto-added help flag is not appended.Before this change, a command with a user-defined
&BoolFlag{Name: "help"}received the built-in help flag too, leaving two flags namedhelpand duplicated help output. After this change, the user-defined help flag wins, overriding the help flag continues to work,--helpstill parses without error, and only onehelpflag is present.Which issue(s) this PR fixes:
Fixes #2174
Special notes for your reviewer:
Compatibility note: user-defined overrides of the built-in help flag continue to work. Only configurations where the user's own
Flagsslice contains duplicate names/aliases now return an error duringRun.Testing
go test -run "TestDuplicateFlagNamesAreRejected|TestUserDefinedHelpFlagOverridesBuiltin" -count=1 -vfails on unmodified source (TestDuplicateFlagNamesAreRejectedgets nil errors;TestUserDefinedHelpFlagOverridesBuiltinfinds 2helpflags) and passes with this fix.go build ./...✅go vet ./...✅gofmt -l .✅ emptygo test ./...✅go run ./scripts/build.go generate✅go run ./scripts/build.go vet✅go run ./scripts/build.go check-binary-size✅ (1.8MBcurrent size; target1.5MB–2.2MB)go run ./scripts/build.go v3diff✅ (with Gitdiffon PATH)Release Notes