github.com/wolfd/bazel-gazelle@v0.14.0/internal/flag/flag.go (about)

     1  // Copyright 2017 The Bazel Authors. All rights reserved.
     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 flag
    16  
    17  import (
    18  	stdflag "flag"
    19  	"strings"
    20  )
    21  
    22  // MultiFlag allows repeated string flags to be collected into a slice
    23  type MultiFlag struct {
    24  	Values *[]string
    25  }
    26  
    27  var _ stdflag.Value = (*MultiFlag)(nil)
    28  
    29  func (m *MultiFlag) Set(v string) error {
    30  	*m.Values = append(*m.Values, v)
    31  	return nil
    32  }
    33  
    34  func (m *MultiFlag) String() string {
    35  	if m == nil || m.Values == nil {
    36  		return ""
    37  	}
    38  	return strings.Join(*m.Values, ",")
    39  }
    40  
    41  // ExplicitFlag is a string flag that tracks whether it was set.
    42  type ExplicitFlag struct {
    43  	IsSet *bool
    44  	Value *string
    45  }
    46  
    47  var _ stdflag.Value = (*ExplicitFlag)(nil)
    48  
    49  func (f *ExplicitFlag) Set(value string) error {
    50  	*f.IsSet = true
    51  	*f.Value = value
    52  	return nil
    53  }
    54  
    55  func (f *ExplicitFlag) String() string {
    56  	if f == nil || f.Value == nil {
    57  		return ""
    58  	}
    59  	return *f.Value
    60  }