OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Package stringsetflag provides a flag.Value implementation which resolves |
| 6 // multiple args into a stringset. |
| 7 package stringsetflag |
| 8 |
| 9 import ( |
| 10 "flag" |
| 11 "fmt" |
| 12 "sort" |
| 13 "strings" |
| 14 |
| 15 "github.com/luci/luci-go/common/stringset" |
| 16 ) |
| 17 |
| 18 // Flag is a flag.Value implementation which represents an unordered set of |
| 19 // strings. |
| 20 // |
| 21 // For example, this allows you to construct a flag that would behave like: |
| 22 // -myflag Foo |
| 23 // -myflag Bar |
| 24 // -myflag Bar |
| 25 // |
| 26 // And then myflag.Data.Has("Bar") would be true. |
| 27 type Flag struct{ Data stringset.Set } |
| 28 |
| 29 var _ flag.Value = (*Flag)(nil) |
| 30 |
| 31 func (f Flag) String() string { |
| 32 if f.Data == nil { |
| 33 return "" |
| 34 } |
| 35 slc := f.Data.ToSlice() |
| 36 sort.Strings(slc) |
| 37 return strings.Join(slc, ",") |
| 38 } |
| 39 |
| 40 // Set implements flag.Value's Set function. |
| 41 func (f *Flag) Set(val string) error { |
| 42 if val == "" { |
| 43 return fmt.Errorf("must have an argument value") |
| 44 } |
| 45 |
| 46 if f.Data == nil { |
| 47 f.Data = stringset.NewFromSlice(val) |
| 48 } else { |
| 49 f.Data.Add(val) |
| 50 } |
| 51 return nil |
| 52 } |
OLD | NEW |