]> Cypherpunks repositories - gostls13.git/commit
Flags: add user-defined flag types. The change is really no code; it's just publishing
authorRob Pike <r@golang.org>
Tue, 30 Mar 2010 00:37:22 +0000 (17:37 -0700)
committerRob Pike <r@golang.org>
Tue, 30 Mar 2010 00:37:22 +0000 (17:37 -0700)
commit570af81e190ecbd06dbe530849ea0d8405a483c4
tree7a2cfe8feaa1261c6294682a3904c1703975f709
parentd99a3da7b26c3423ff94cd7cea5cb739f8d7c4db
Flags: add user-defined flag types. The change is really no code; it's just publishing
the set() method and add() functions.  But we rename add() to Var() for consistency.
Also rename FlagValue to Value for simplicity.

Also, delete the check for multiple settings for a flag.  This makes it possible to
define a flag that collects values, such as into a slice of strings.

type flagVar []string

func (f *flagVar) String() string {
return fmt.Sprint(v)
}

func (f *flagVar) Set(value string) bool {
if v == nil {
v = make(flagVar, 1)
} else {
nv := make(flagVar, len(v)+1)
copy(nv, v)
v = nv
}
v[len(v)-1] = value
return true
}

var v flagVar

func main() {
flag.Var(&v, "testV", "multiple values build []string")
flag.Parse()
fmt.Printf("v = %v\n", v)
}

R=rsc
CC=golang-dev
https://golang.org/cl/842041
src/pkg/flag/flag.go
src/pkg/flag/flag_test.go