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

Unexpected flag value when asterisk (*) is passed as string flag value #406

Open
jakedoublev opened this issue Jun 11, 2024 · 2 comments
Open

Comments

@jakedoublev
Copy link

When a single or series of asterisks (i.e. * or ***) are passed as the values of string flags, pflag will return a file name within the current directory instead of the asterisk strings.

Code to reproduce:

package main

import (
	"fmt"

	flags "github.com/spf13/pflag"
)

var coolflag string

func init() {
	flags.StringVar(&coolflag, "coolflag", "", "This is a cool flag")
}

func main() {
	flags.Parse()
	if coolflag == "" {
		flags.PrintDefaults()
		return
	}
	fmt.Println("cool flag value: ", coolflag)
}

Execution is in a directory with the above code in main.go, a go.mod like the below, and the generated go.sum:

module flagstest

go 1.22.3

require github.com/spf13/pflag v1.0.5

Results:

jake.vanvorhis$ go run . --coolflag *
cool flag value:  go.mod
jake.vanvorhis$ go run . --coolflag ***
cool flag value:  go.mod
jake.vanvorhis$ go run . --coolflag hello
cool flag value:  hello

I have also seen this reproduced where CODEOWNERS was the file read into the string flag value.

@Agent-E11
Copy link

Agent-E11 commented Jun 24, 2024

I believe this is actually the shell expanding the glob * before passing the arguments, and it doesn't have anything to do with pflag. Try wrapping the asterisk in ':

go run . --coolflag '*'

@Agent-E11
Copy link

The same thing happens with echo:

$ echo *
go.mod go.sum main.go
$ echo '*'
*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants