blob: 3e47fb38e193ec2c9ac42f12bd0f2fa7fc16e147 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001// 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
15package flags
16
17import (
18 "flag"
19 "sort"
20 "strings"
21)
22
23// StringsValue wraps "sort.StringSlice".
24type StringsValue sort.StringSlice
25
26// Set parses a command line set of strings, separated by comma.
27// Implements "flag.Value" interface.
28func (ss *StringsValue) Set(s string) error {
29 *ss = strings.Split(s, ",")
30 return nil
31}
32
33// String implements "flag.Value" interface.
34func (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.
38func 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.
50func StringsFromFlag(fs *flag.FlagSet, flagName string) []string {
51 return []string(*fs.Lookup(flagName).Value.(*StringsValue))
52}