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

proposal: flag: add slice flag #67247

Closed
zkep opened this issue May 8, 2024 · 2 comments
Closed

proposal: flag: add slice flag #67247

zkep opened this issue May 8, 2024 · 2 comments
Labels
Milestone

Comments

@zkep
Copy link

zkep commented May 8, 2024

Proposal Details

Proposal Details

In practice, array-type fields are often used, and they should be supported, perhaps not necessarily required, but useful

Such as string slice, using a single name to accept multiple values, such a requirement exists in command-line programs

// -- strings Value
type stringsValue []string

func newStringsValue(val []string, p *[]string) *stringsValue {
	*p = val
	return (*stringsValue)(p)
}

func (s *stringsValue) Set(val string) error {
	*s = append(*s, val)
	return nil
}

func (s *stringsValue) Get() any { return []string(*s) }

func (s *stringsValue) String() string { return fmt.Sprint([]string(*s)) }

// StringsVar defines many string flag with specified name, default value, and usage string.
// The argument p points to many string variable in which to store the value of the flag.
func (f *FlagSet) StringsVar(p *[]string, name string, value []string, usage string) {
	f.Var(newStringsValue(value, p), name, usage)
}


// StringsVar defines many string flag with specified name, default value, and usage string.
// The argument p points to many string variable in which to store the value of the flag.
func StringsVar(p *[]string, name string, value []string, usage string) {
	CommandLine.Var(newStringsValue(value, p), name, usage)
}

// Strings defines many string flag with specified name, default value, and usage string.
// The return value is the address of many string variable that stores the value of the flag.
func (f *FlagSet) Strings(name string, value []string, usage string) *[]string {
	p := new([]string)
	f.StringsVar(p, name, value, usage)
	return p
}

// Strings defines many string flag with specified name, default value, and usage string.
// The return value is the address of many string variable that stores the value of the flag.
func Strings(name string, value []string, usage string) *[]string {
	return CommandLine.Strings(name, value, usage)
}
@zkep zkep added the Proposal label May 8, 2024
@gopherbot gopherbot added this to the Proposal milestone May 8, 2024
@ianlancetaylor ianlancetaylor changed the title proposal: flag add slice flag proposal: flag: add slice flag May 8, 2024
@ianlancetaylor
Copy link
Contributor

CC @robpike

@robpike
Copy link
Contributor

robpike commented May 8, 2024

Dup of #40155

@robpike robpike closed this as completed May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants