Skip to content

The boolean flag is not behaving as intended. #2206

@lignumqt

Description

@lignumqt

My urfave/cli version is

cli/v3

Checklist

  • Are you running the latest v3 release? The list of releases is here.
  • Did you check the manual for your release? The v3 manual is here
  • 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 =:

Flag values can be provided with a space after the flag name or using the ```=``` sign
.

But in the code, I found that this is ignored:

flagVal = "true"

trace:

## URFAVE CLI TRACE github.com/urfave/cli/v3@v3.4.1/command_parse.go:79 (github.com/urfave/cli/v3.(*Command).parseFlags) rearrange:1 (cmd="modify") ["--fooBar" "false"]
## URFAVE CLI TRACE github.com/urfave/cli/v3@v3.4.1/command_parse.go:128 (github.com/urfave/cli/v3.(*Command).parseFlags) parseFlags (shortOptionHandling=%!q(bool=false))
## URFAVE CLI TRACE github.com/urfave/cli/v3@v3.4.1/command_parse.go:132 (github.com/urfave/cli/v3.(*Command).parseFlags) flagName:1 (fName="fooBar")
## URFAVE CLI TRACE github.com/urfave/cli/v3@v3.4.1/command_parse.go:142 (github.com/urfave/cli/v3.(*Command).parseFlags) flagName:2 (fName="fooBar") (fVal="")
## URFAVE CLI TRACE github.com/urfave/cli/v3@v3.4.1/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/v3@v3.4.1/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/v3@v3.4.1/command_parse.go:79 (github.com/urfave/cli/v3.(*Command).parseFlags) rearrange:1 (cmd="modify") ["false"]
## URFAVE CLI TRACE github.com/urfave/cli/v3@v3.4.1/command_parse.go:95 (github.com/urfave/cli/v3.(*Command).parseFlags) rearrange-3 (cmd="modify") check "false"
## URFAVE CLI TRACE github.com/urfave/cli/v3@v3.4.1/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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/v3relates to / is being considered for v3kind/bugdescribes or fixes a bugstatus/triagemaintainers still need to look into this

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions