github.com/fafucoder/cilium@v1.6.11/pkg/monitor/format/flags.go (about)

     1  // Copyright 2018 Authors of Cilium
     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 format
    16  
    17  import (
    18  	"strconv"
    19  	"strings"
    20  
    21  	"github.com/spf13/pflag"
    22  )
    23  
    24  // Uint16Flags is a slice of unsigned 16-bit ints with some convenience methods.
    25  type Uint16Flags []uint16
    26  
    27  var _ pflag.Value = &Uint16Flags{}
    28  
    29  // String provides a human-readable string format of the received variable.
    30  func (i *Uint16Flags) String() string {
    31  	pieces := make([]string, 0, len(*i))
    32  	for _, v := range *i {
    33  		pieces = append(pieces, strconv.Itoa(int(v)))
    34  	}
    35  	return strings.Join(pieces, ", ")
    36  }
    37  
    38  // Set converts the specified value into an integer and appends it to the flags.
    39  // Returns an error if the value cannot be converted to a 16-bit unsigned value.
    40  func (i *Uint16Flags) Set(value string) error {
    41  	v, err := strconv.Atoi(value)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	*i = append(*i, uint16(v))
    46  	return nil
    47  }
    48  
    49  // Type returns a human-readable string representing the type of the receiver.
    50  func (i *Uint16Flags) Type() string {
    51  	return "[]uint16"
    52  }
    53  
    54  // Has returns true of value exist
    55  func (i *Uint16Flags) Has(value uint16) bool {
    56  	for _, v := range *i {
    57  		if v == value {
    58  			return true
    59  		}
    60  	}
    61  
    62  	return false
    63  }