Skip to content

StringSlice trims leading and tailing spaces #1648

@palsivertsen

Description

@palsivertsen

My urfave/cli version is

v2.23.7

Checklist

  • Are you running the latest v2 release? The list of releases is here.
  • Did you check the manual for your release? The v2 manual is here <-- This link is broken.
  • Did you perform a search about this problem? Here's the GitHub guide about searching.

Dependency Management

  • My project is using go modules.

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/v2relates to / is being considered for v2kind/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