Skip to content

Instantly share code, notes, and snippets.

@andrewmeissner
Last active December 8, 2020 19:09
Show Gist options
  • Select an option

  • Save andrewmeissner/e52ec19a8b8d253ee42a508c63cdbf2a to your computer and use it in GitHub Desktop.

Select an option

Save andrewmeissner/e52ec19a8b8d253ee42a508c63cdbf2a to your computer and use it in GitHub Desktop.
CLI array flags using stdlib
type arrayFlags []string
func (i *arrayFlags) String() string {
return strings.Join(*i, ",")
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, strings.Split(value, ",")...)
return nil
}
var sliceOfStrings arrayFlags
// where you declare your flags
flag.Var(&sliceOfStrings, "flag-name", "flag description")
// thisThing -flag-name hello -flag-name world
// |__ sliceOfStrings == []string{"hello", "world"}
// thisThing -flag-name hello,world
// |__ sliceOfStrings == []string{"hello", "world"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment