go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/flag/stringlistflag/stringlistflag.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package stringlistflag provides a flag.Value implementation which resolves 16 // multiple args into a []string. 17 package stringlistflag 18 19 import ( 20 "flag" 21 "fmt" 22 "strings" 23 ) 24 25 // Flag is a flag.Value implementation which represents an ordered set of 26 // strings. 27 // 28 // For example, this allows you to construct a flag that would behave like: 29 // 30 // -myflag Foo 31 // -myflag Bar 32 // -myflag Bar 33 // 34 // And then myflag would be []string{"Foo", "Bar", "Bar"} 35 type Flag []string 36 37 var _ flag.Value = (*Flag)(nil) 38 39 func (f Flag) String() string { 40 return strings.Join(f, ", ") 41 } 42 43 // Set implements flag.Value's Set function. 44 func (f *Flag) Set(val string) error { 45 if val == "" { 46 return fmt.Errorf("must have an argument value") 47 } 48 49 *f = append(*f, val) 50 return nil 51 }