...

Source file src/go.etcd.io/etcd/pkg/v3/flags/strings.go

Documentation: go.etcd.io/etcd/pkg/v3/flags

     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  	"fmt"
    20  	"sort"
    21  	"strings"
    22  )
    23  
    24  // StringsValue wraps "sort.StringSlice".
    25  type StringsValue sort.StringSlice
    26  
    27  // Set parses a command line set of strings, separated by comma.
    28  // Implements "flag.Value" interface.
    29  func (ss *StringsValue) Set(s string) error {
    30  	*ss = strings.Split(s, ",")
    31  	return nil
    32  }
    33  
    34  // String implements "flag.Value" interface.
    35  func (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.
    39  func 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.
    51  func StringsFromFlag(fs *flag.FlagSet, flagName string) []string {
    52  	return []string(*fs.Lookup(flagName).Value.(*StringsValue))
    53  }
    54  

View as plain text