khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | // Copyright 2018 The etcd 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 flags |
| 16 | |
| 17 | import ( |
| 18 | "flag" |
| 19 | "sort" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | // StringsValue wraps "sort.StringSlice". |
| 24 | type StringsValue sort.StringSlice |
| 25 | |
| 26 | // Set parses a command line set of strings, separated by comma. |
| 27 | // Implements "flag.Value" interface. |
| 28 | func (ss *StringsValue) Set(s string) error { |
| 29 | *ss = strings.Split(s, ",") |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | // String implements "flag.Value" interface. |
| 34 | func (ss *StringsValue) String() string { return strings.Join(*ss, ",") } |
| 35 | |
| 36 | // NewStringsValue implements string slice as "flag.Value" interface. |
| 37 | // Given value is to be separated by comma. |
| 38 | func NewStringsValue(s string) (ss *StringsValue) { |
| 39 | if s == "" { |
| 40 | return &StringsValue{} |
| 41 | } |
| 42 | ss = new(StringsValue) |
| 43 | if err := ss.Set(s); err != nil { |
| 44 | plog.Panicf("new StringsValue should never fail: %v", err) |
| 45 | } |
| 46 | return ss |
| 47 | } |
| 48 | |
| 49 | // StringsFromFlag returns a string slice from the flag. |
| 50 | func StringsFromFlag(fs *flag.FlagSet, flagName string) []string { |
| 51 | return []string(*fs.Lookup(flagName).Value.(*StringsValue)) |
| 52 | } |