github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/cmd/gazelle/flags.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 main
    16  
    17  import "fmt"
    18  
    19  // multiFlag allows repeated string flags to be collected into a slice
    20  type multiFlag []string
    21  
    22  func (m *multiFlag) String() string {
    23  	if len(*m) == 0 {
    24  		return ""
    25  	}
    26  	return fmt.Sprint(*m)
    27  }
    28  
    29  func (m *multiFlag) Set(v string) error {
    30  	(*m) = append(*m, v)
    31  	return nil
    32  }
    33  
    34  // explicitFlag is a string flag that tracks whether it was set.
    35  type explicitFlag struct {
    36  	set   bool
    37  	value string
    38  }
    39  
    40  func (f *explicitFlag) Set(value string) error {
    41  	f.set = true
    42  	f.value = value
    43  	return nil
    44  }
    45  
    46  func (f *explicitFlag) String() string {
    47  	if f == nil {
    48  		return ""
    49  	}
    50  	return f.value
    51  }