Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 bug: default value changes with parsed values on slice flags #1235

Closed
5 of 7 tasks
vipally opened this issue Feb 5, 2021 · 1 comment · Fixed by #1236
Closed
5 of 7 tasks

v2 bug: default value changes with parsed values on slice flags #1235

vipally opened this issue Feb 5, 2021 · 1 comment · Fixed by #1236
Labels
area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this

Comments

@vipally
Copy link
Contributor

vipally commented Feb 5, 2021

my urfave/cli version is

v2

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
  • Did you perform a search about this problem? Here's the Github guide about searching.

Dependency Management

  • My project is using go modules.
  • My project is using vendoring.
  • My project is automatically downloading the latest version.
  • I am unsure of what my dependency management setup is.

Describe the bug

default value changes with parsed values on slice flags.

To reproduce

I have put the test code here

package main

import (
	"fmt"

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

func main() {
	app := &cli.App{
		Name: "cli_bug",
		Flags: []cli.Flag{
			&cli.BoolFlag{
				Name:    "bool",
				Value:   false,
				Aliases: []string{"b"},
			},
			&cli.StringFlag{
				Name:    "string",
				Aliases: []string{"s"},
				Value:   "default",
			},
			&cli.IntFlag{
				Name:    "int",
				Aliases: []string{"i"},
				Value:   1,
			},
			&cli.StringSliceFlag{
				Name:    "stringSlice",
				Aliases: []string{"ss"},
				Value:   cli.NewStringSlice("default1", "default2"),
			},
			&cli.Float64SliceFlag{
				Name:    "float64Slice",
				Aliases: []string{"f64s"},
				Value:   cli.NewFloat64Slice(1.1, 2.2),
			},
			&cli.Int64SliceFlag{
				Name:    "int64Slice",
				Aliases: []string{"i64s"},
				Value:   cli.NewInt64Slice(1, 2),
			},
			&cli.IntSliceFlag{
				Name:    "intSlice",
				Aliases: []string{"is"},
				Value:   cli.NewIntSlice(1, 2),
			},
		},
		Action: func(ctx *cli.Context) error {
			fmt.Println("FlagNames", ctx.FlagNames())
			for i, v := range ctx.FlagNames() {
				fmt.Printf("%d %s %+v\n", i, v, ctx.Value(v))
			}
			err := ctx.Err()
			fmt.Println("error", err)
			if err == nil {
				cli.ShowAppHelp(ctx)
			}
			return nil
		},
	}
	args := []string{
		"lab23",
		"-b", // "false",
		"--string", "setstring1",
		"--int", "13",
		"--stringSlice", "stringSlice",
		"--float64Slice", "13.3",
		"--int64Slice", "13",
		"--intSlice", "13",
	}
	fmt.Printf("args: %+v\n\n", args)
	if err := app.Run(args); err != nil {
		panic(err)
	}

	/*
		GLOBAL OPTIONS:
		   --bool, -b                          (default: false)
		   --string value, -s value            (default: "default")
		   --int value, -i value               (default: 1)
		   --stringSlice value, --ss value     (default: "stringSlice") // bug expect: (default: "default1", "default2")
		   --float64Slice value, --f64s value  (default: 13.3)			// bug expect: (default: 1.1, 2.2)
		   --int64Slice value, --i64s value    (default: 13)			// bug expect: (default: 1, 2)
		   --intSlice value, --is value        (default: 13)			// bug expect: (default: 1, 2)
		   --help, -h                          show help (default: false)
	*/
}

Observed behavior

The default value of slices changes from the parsed value:
--stringSlice value, --ss value (default: "stringSlice") // bug expect: (default: "default1", "default2")
--float64Slice value, --f64s value (default: 13.3) // bug expect: (default: 1.1, 2.2)
--int64Slice value, --i64s value (default: 13) // bug expect: (default: 1, 2)
--intSlice value, --is value (default: 13) // bug expect: (default: 1, 2)

Expected behavior

Expected default values:
--stringSlice value, --ss value (default: "default1", "default2")
--float64Slice value, --f64s value (default: 1.1, 2.2)
--int64Slice value, --i64s value (default: 1, 2)
--intSlice value, --is value (default: 1, 2)

Additional context

Add any other context about the problem here.

If the issue relates to a specific open source Github repo, please link that repo here.

If you can reproduce this issue with a public CI system, please link a failing build here.

Want to fix this yourself?

We'd love to have more contributors on this project! If the fix for this bug is easily explained and very small, free free to create a pull request for it.

Run go version and paste its output here

go version go1.15.3 windows/amd64

Run go env and paste its output here

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Admin\AppData\Local\go-build
set GOENV=C:\Users\Admin\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\dev\gocode\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\dev\gocode
set GOPRIVATE=
set GOPROXY=https://goproxy.cn
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\Admin\AppData\Local\Temp\go-build849182602=/tmp/go-build -gno-record-gcc-switches
@vipally vipally added area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this labels Feb 5, 2021
vipally added a commit to vipally/cli that referenced this issue Feb 5, 2021
@vipally
Copy link
Contributor Author

vipally commented Feb 5, 2021

maybe related to #1206

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant