blob: e3d131f790251aad6abe741be37a973dbe76f7ed [file] [log] [blame]
Abhay Kumara2ae5992025-11-10 14:02:24 +00001// 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 "fmt"
20 "sort"
21 "strings"
22)
23
24// StringsValue wraps "sort.StringSlice".
25type StringsValue sort.StringSlice
26
27// Set parses a command line set of strings, separated by comma.
28// Implements "flag.Value" interface.
29func (ss *StringsValue) Set(s string) error {
30 *ss = strings.Split(s, ",")
31 return nil
32}
33
34// String implements "flag.Value" interface.
35func (ss *StringsValue) String() string { return strings.Join(*ss, ",") }
36
37// NewStringsValue implements string slice as "flag.Value" interface.
38// Given value is to be separated by comma.
39func NewStringsValue(s string) (ss *StringsValue) {
40 if s == "" {
41 return &StringsValue{}
42 }
43 ss = new(StringsValue)
44 if err := ss.Set(s); err != nil {
45 panic(fmt.Sprintf("new StringsValue should never fail: %v", err))
46 }
47 return ss
48}
49
50// StringsFromFlag returns a string slice from the flag.
51func StringsFromFlag(fs *flag.FlagSet, flagName string) []string {
52 return *fs.Lookup(flagName).Value.(*StringsValue)
53}