Skip to content

fix: use FlagNamePrefixer and FlagEnvHinter in the default flag stringer - #2494

Merged
Juneezee merged 1 commit into
urfave:mainfrom
vidigoat:fix/flag-hint-hooks
Oct 24, 2026
Merged

Juneezee merged 1 commit into
urfave:mainfrom
vidigoat:fix/flag-hint-hooks

Conversation

@vidigoat

@vidigoat vidigoat commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • bug

What this PR does / why we need it:

FlagNamePrefixer and FlagEnvHinter are exported variables documented as "used by the default FlagStringer", but stringifyFlag calls prefixedNames and withEnvHint directly, so setting them changes nothing:

cli.FlagEnvHinter = func(envVars []string, str string) string {
	return str + " (env: " + strings.Join(envVars, ", ") + ")"
}
// help still shows:  --token string   [$TOKEN]

stringifyFlag now calls the two variables. Their defaults are prefixedNames and withEnvHint, so help output only changes for programs that set them. This includes the per-flag Stringer override path, since that falls back to FlagStringer.

FlagFileHinter has the same comment, but help doesn't show file sources at all today (FlagFileHinter and withFileHint haven't been called since v2), so using it would add text to the help of every flag with a File source. Instead, its comment and FlagFileHintFunc's now say the default FlagStringer doesn't use it (the old comment also said "environment variable details").

Which issue(s) this PR fixes:

None, found while reading the help code.

Testing

  • TestFlagEnvHinter had been commented out since the v2 merge (its table was later renamed to var _ to silence the linter). It's restored for v3 (Sources, the string placeholder), with a new TestFlagNamePrefixer next to it. Both fail on main.
  • make (generate, vet, tests, binary size, gfmrun), make lint and make v3diff pass. go test -race passes, as do the tests with urfave_cli_no_template, urfave_cli_no_docs and urfave_cli_no_suggest.

The change and the tests were written with Claude Code (AI).

Release Notes

Setting FlagNamePrefixer or FlagEnvHinter now changes the default flag help text, as documented.

🤖 Generated with Claude Code

@vidigoat
vidigoat requested a review from a team as a code owner October 24, 2026 07:34
@Juneezee
Juneezee force-pushed the fix/flag-hint-hooks branch from 4646dad to 86c0618 Compare October 24, 2026 15:53
Both are documented as "used by the default FlagStringer", but
stringifyFlag called prefixedNames and withEnvHint directly, so setting
them had no effect. Their defaults are those same functions, so the
default help output doesn't change.

FlagFileHinter and FlagFileHintFunc had the same claim, but nothing has
called FlagFileHinter since v2, so their comments now say the default
FlagStringer doesn't use it.

TestFlagEnvHinter, commented out since the v2 merge, is restored for v3,
with a TestFlagNamePrefixer next to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@Juneezee
Juneezee force-pushed the fix/flag-hint-hooks branch from 86c0618 to b134c57 Compare October 24, 2026 16:13

@Juneezee Juneezee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Confirmed on main: setting FlagEnvHinter or FlagNamePrefixer doesn't change the help output, and with this PR it does. Both new tests fail on main and pass here.

I pushed a few changes:

  • Rebased onto main.
  • Removed the table from the old TestFlagNamePrefixer, which was still a dead var _. Your new TestFlagNamePrefixer replaces it.
  • Reworded the FlagFileHinter and FlagFileHintFunc comments to say the default FlagStringer doesn't use them. Showing file sources would change the help of every flag with a File source, so I kept this to the comments. I also updated the description to match.

@Juneezee
Juneezee merged commit 4b0f1f4 into urfave:main Oct 24, 2026
9 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