lab.nexedi.com/kirr/go123@v0.0.0-20240207185015-8299741fa871/xflag/xflag.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE-go file.
     4  
     5  package xflag
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  )
    11  
    12  // originally taken from cmd/dist.count in go.git
    13  
    14  // Count is a flag.Value that is like a flag.Bool and a flag.Int.
    15  // If used as -name, it increments the Count, but -name=x sets the Count.
    16  // Used for verbose flag -v.
    17  type Count int
    18  
    19  func (c *Count) String() string {
    20  	return fmt.Sprint(int(*c))
    21  }
    22  
    23  func (c *Count) Set(s string) error {
    24  	switch s {
    25  	case "true":
    26  		*c++
    27  	case "false":
    28  		*c = 0
    29  	default:
    30  		n, err := strconv.Atoi(s)
    31  		if err != nil {
    32  			return fmt.Errorf("invalid count %q", s)
    33  		}
    34  		*c = Count(n)
    35  	}
    36  	return nil
    37  }
    38  
    39  func (c *Count) IsBoolFlag() bool {
    40  	return true
    41  }