urfave / cli

A simple, fast, and fun package for building command line apps in Go
https://cli.urfave.org
MIT License
22.38k stars 1.71k forks source link

StringSlice trims leading and tailing spaces #1648

Closed palsivertsen closed 1 year ago

palsivertsen commented 1 year ago

My urfave/cli version is

v2.23.7

Checklist

Dependency Management

Describe the bug

I expect cli.StringSliceFlag to accept leading and tailing spaces, similar to the behavior of cli.StringFlag.

To reproduce

The following program (see Go Playground) shows that cli.StringSlice trims leading and tailing spaces in flag values.

package main

import (
    "fmt"
    "log"

    "github.com/urfave/cli/v2"
)

const expected = " <- spaces -> "

func main() {
    app := cli.App{
        Flags: []cli.Flag{
            &cli.StringFlag{Name: "string"},
            &cli.StringSliceFlag{Name: "slice"},
        },
        Action: func(ctx *cli.Context) error {
            s1, s2 := ctx.String("string"), ctx.StringSlice("slice")

            if len(s2) == 1 && s1 == s2[0] {
                return nil
            }

            _, err := fmt.Fprintf(ctx.App.Writer, "%q != %q", ctx.String("string"), ctx.StringSlice("slice")[0])
            return err
        },
    }

    if err := app.Run([]string{"", "--string", expected, "--slice", expected}); err != nil {
        log.Fatalf("app run: %s", err)
    }
}

Observed behavior

The value was trimmed of leading and tailing spaces.

Expected behavior

The value should be unchanged.

Additional context

Discovered while passing a regexp string as a flag value (^).

Want to fix this yourself?

Maybe

Run go version and paste its output here

go version go1.19.4 linux/amd64

Run go env and paste its output here

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/pal/.cache/go-build"
GOENV="/home/pal/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/pal/projects/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/pal/projects/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/tmp/tmp.mbFOk4K7cf/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3921918458=/tmp/go-build -gno-record-gcc-switches"
skelouse commented 1 year ago

https://github.com/urfave/cli/pull/1649