cli icon indicating copy to clipboard operation
cli copied to clipboard

The boolean flag is not behaving as intended.

Open lignumqt opened this issue 3 months ago • 1 comments

My urfave/cli version is

cli/v3

Checklist

  • [X] Are you running the latest v3 release? The list of releases is here.
  • [X] Did you check the manual for your release? The v3 manual is here
  • [X] Did you perform a search about this problem? Here's the GitHub guide about searching.

Describe the bug

I'm using a Boolean flag and want to set it to 1/0 or true/false. But if I separate it with a space, the value isn't read and defaults to true. The same thing happens if I omit the value. But if I use key=value separated by the = sign, everything works correctly.

The documentation says I can use both: a space or =: https://github.com/urfave/cli/blob/19d941b4749b4d9878033b80d29bdc5e2916a62a/docs/v3/examples/flags/basics.md?plain=1#L73.

But in the code, I found that this is ignored: https://github.com/urfave/cli/blob/19d941b4749b4d9878033b80d29bdc5e2916a62a/command_parse.go#L153

trace:

## URFAVE CLI TRACE github.com/urfave/cli/[email protected]/command_parse.go:79 (github.com/urfave/cli/v3.(*Command).parseFlags) rearrange:1 (cmd="modify") ["--fooBar" "false"]
## URFAVE CLI TRACE github.com/urfave/cli/[email protected]/command_parse.go:128 (github.com/urfave/cli/v3.(*Command).parseFlags) parseFlags (shortOptionHandling=%!q(bool=false))
## URFAVE CLI TRACE github.com/urfave/cli/[email protected]/command_parse.go:132 (github.com/urfave/cli/v3.(*Command).parseFlags) flagName:1 (fName="fooBar")
## URFAVE CLI TRACE github.com/urfave/cli/[email protected]/command_parse.go:142 (github.com/urfave/cli/v3.(*Command).parseFlags) flagName:2 (fName="fooBar") (fVal="")
## URFAVE CLI TRACE github.com/urfave/cli/[email protected]/command_parse.go:147 (github.com/urfave/cli/v3.(*Command).parseFlags) Trying flag type (fName="fooBar") (type=*cli.FlagBase[bool,github.com/urfave/cli/v3.BoolConfig,github.com/urfave/cli/v3.boolValue])
## URFAVE CLI TRACE github.com/urfave/cli/[email protected]/command_parse.go:152 (github.com/urfave/cli/v3.(*Command).parseFlags) parse Apply bool flag (fName="fooBar") (fVal="true")
## URFAVE CLI TRACE github.com/urfave/cli/[email protected]/command_parse.go:79 (github.com/urfave/cli/v3.(*Command).parseFlags) rearrange:1 (cmd="modify") ["false"]
## URFAVE CLI TRACE github.com/urfave/cli/[email protected]/command_parse.go:95 (github.com/urfave/cli/v3.(*Command).parseFlags) rearrange-3 (cmd="modify") check "false"
## URFAVE CLI TRACE github.com/urfave/cli/[email protected]/command_parse.go:215 (github.com/urfave/cli/v3.(*Command).parseFlags) returning-2 (cmd="modify") args ["false"]

To reproduce

create bool flag

cli.BoolFlag{
		Name:        "fooBar",
		DefaultText: "fooBarDefault",
},

run: your_cli --fooBar false

Observed behavior

fooBar is set true(by default)

Expected behavior

The behavior will be as expected from the documentation,i.e. the flag will be false, or the documentation will be corrected

Run go version and paste its output here

# go version go1.25.1 linux/amd64

Run go env and paste its output here

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN='/home/foo/go/bin'
GOCACHE='/home/foo/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/foo/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3008136515=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/foo/ctl/go.mod'
GOMODCACHE='/home/foo/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/foo/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/foo/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.1'
GOWORK=''
PKG_CONFIG='pkg-config'

lignumqt avatar Oct 08 '25 08:10 lignumqt

@lignumqt Yes the docs are correct but should be updated to say for all flags except bool flags !!!.

dearchap avatar Oct 08 '25 15:10 dearchap