Skip to content

fix: don't put a space before the env var hint of a flag with no usage - #2529

Open
vidigoat wants to merge 1 commit into
urfave:mainfrom
vidigoat:fix/env-hint-without-usage
Open

vidigoat wants to merge 1 commit into
urfave:mainfrom
vidigoat:fix/env-hint-without-usage

Conversation

@vidigoat

@vidigoat vidigoat commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • bug

What this PR does / why we need it:

A flag with an env var but no usage text printed its env var hint one column to the right of the other flags' usage:

cmd := &cli.Command{
	Name: "app",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "token", Sources: cli.EnvVars("APP_TOKEN")},
		&cli.StringFlag{Name: "name", Usage: "your name", Sources: cli.EnvVars("APP_NAME")},
		&cli.BoolFlag{Name: "quiet", Sources: cli.EnvVars("APP_QUIET")},
	},
}
GLOBAL OPTIONS:
   --token string   [$APP_TOKEN]
   --name string   your name [$APP_NAME]
   --quiet          [$APP_QUIET]
   --help, -h      show help

stringifyFlag passes "<names>\t<usage>" to FlagEnvHinter, and withEnvHint appends " [$APP_TOKEN]". With no usage, that space is the first thing in the usage column, which the tabwriter aligns. stringifyFlag already trims the space in front of (default: …) for the same case (strings.TrimSpace(usage + defaultValueString)), but the env hint is added after that.

  • docs.go: withEnvHint leaves out the leading space when the string it is given ends with the tab, i.e. the flag has no usage text. A custom FlagEnvHinter gets the same string as before.
  • flag_test.go: TestFlagEnvHintWithoutUsage checks a string flag and a bool flag with two env vars, building the expected hint with withEnvHint so it holds for the Windows %VAR% form too. TestFlagDefaultValueWithEnv and the *WithEnvVarHelpOutput tests built their expected strings by concatenating withEnvHint(envVars, ""), which assumes the hint always starts with a space. They now pass the flag's text to withEnvHint (withEnvHint([]string{"uflag"}, "--flag\t")), or compare against the hint without its leading space.

With this change:

GLOBAL OPTIONS:
   --token string  [$APP_TOKEN]
   --name string   your name [$APP_NAME]
   --quiet         [$APP_QUIET]
   --help, -h      show help

Which issue(s) this PR fixes:

None, found while checking the help output of flags with value sources.

Testing

  • TestFlagEnvHintWithoutUsage fails on main ("--token string\t [$APP_TOKEN]") and passes with this change.
  • make all, make test GFLAGS='--tags urfave_cli_no_template', make lint, make diffcheck and make v3diff pass, and so do the urfave_cli_no_docs and urfave_cli_no_suggest tags.
  • The example CLI built for linux/amd64 with Go 1.27.3 grows by 80 bytes.

Release Notes

In help, the env var hint of a flag with no usage text now lines up with the other flags' usage.

🤖 Generated with Claude Code

A flag with no usage text printed its env var hint one column to the
right of every other flag's usage, because the hint always starts with
a space. stringifyFlag already trims the space in front of the default
text for the same reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vidigoat
vidigoat requested a review from a team as a code owner November 7, 2026 17:56

This branch has not been deployed

No deployments
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.

1 participant