Skip to content

fix: treat "-vn=" in a short option group as an empty value - #2454

Open
kevin9327 wants to merge 1 commit into
urfave:mainfrom
kevin9327:fix/short-group-empty-equals
Open

kevin9327 wants to merge 1 commit into
urfave:mainfrom
kevin9327:fix/short-group-empty-equals

Conversation

@kevin9327

Copy link
Copy Markdown

What type of PR is this?

  • bug

What this PR does / why we need it:

#2297 (for #2293) made --name= set the flag to an empty string instead of reading the next argument as its value. The short option group path in parseFlags was not changed, so with UseShortOptionHandling the same = behaves differently once the flag is the last member of a group:

cmd := &cli.Command{
	UseShortOptionHandling: true,
	Flags: []cli.Flag{
		&cli.BoolFlag{Name: "v"},
		&cli.StringFlag{Name: "n", Value: "default"},
	},
}
args n before n after
app -n= positional "", args [positional] unchanged
app -vn= positional "positional", args [] "", args [positional]
app -vn= error flag needs an argument: n ""
  • command_parse.go: the last flag of a short option group only reads the next argument when there was no = in the token, which is the same !valFromEqual check the single flag path already has.
  • command_test.go: two subtests in TestFlagEqualsEmptyValue, one for -vn= positional and one for -vn= at the end of the args.

Which issue(s) this PR fixes:

None filed. I found this while reading the short option code next to the #2297 change and couldn't find an existing issue or PR about it.

Testing

  • The two new subtests fail on main (the positional is consumed as the value; flag needs an argument: n) and pass with the change.
  • make generate vet test gfmrun passes. make generate rewrites a few doc links in godoc-current.txt with my local Go 1.24, on main too, so I left that file alone.

Release Notes

With short option handling, `-vn=` in a short option group now sets `n` to an empty string, as `-n=` and `--name=` do, instead of reading the next argument as its value.

@kevin9327
kevin9327 requested a review from a team as a code owner October 4, 2026 21:40
@greptile-apps

greptile-apps Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Fixes command-line flag parsing for empty values in option groups.

The PR appears safe to merge.

Summary

The PR makes an explicit empty value on the last flag in a short-option group behave like an explicit empty value on a single flag.

  • Adds tests for -vn= with and without a following positional argument.

Reviews (1) · Last reviewed commit: "fix: treat "-vn=" in a short option grou..."

Comment thread command_test.go
assert.Equal(t, []string{"positional"}, args)
})

t.Run("-vn= in a short option group sets empty string", func(t *testing.T) {

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.

👍🏼 love it

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.

2 participants