1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // This logic was copied from stringsFlag from $GOROOT/src/cmd/go/build.go.
11 const TagsFlagDoc = "a list of `build tags` to consider satisfied during the build. " +
12 "For more information about build tags, see the description of " +
13 "build constraints in the documentation for the go/build package"
15 // TagsFlag is an implementation of the flag.Value and flag.Getter interfaces that parses
16 // a flag value in the same manner as go build's -tags flag and
17 // populates a []string slice.
19 // See $GOROOT/src/go/build/doc.go for description of build tags.
20 // See $GOROOT/src/cmd/go/doc.go for description of 'go build -tags' flag.
23 // flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
24 type TagsFlag []string
26 func (v *TagsFlag) Set(s string) error {
28 *v, err = splitQuotedFields(s)
35 func (v *TagsFlag) Get() interface{} { return *v }
37 func splitQuotedFields(s string) ([]string, error) {
38 // Split fields allowing '' or "" around elements.
39 // Quotes further inside the string do not count.
42 for len(s) > 0 && isSpaceByte(s[0]) {
48 // Accepted quoted string. No unescaping inside.
49 if s[0] == '"' || s[0] == '\'' {
53 for i < len(s) && s[i] != quote {
57 return nil, fmt.Errorf("unterminated %c string", quote)
64 for i < len(s) && !isSpaceByte(s[i]) {
73 func (v *TagsFlag) String() string {
77 func isSpaceByte(c byte) bool {
78 return c == ' ' || c == '\t' || c == '\n' || c == '\r'