github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/cmd/internal/objabi/flag.go (about)

     1  // Copyright 2015 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 file.
     4  
     5  package objabi
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  	"strconv"
    12  	"strings"
    13  )
    14  
    15  func Flagcount(name, usage string, val *int) {
    16  	flag.Var((*count)(val), name, usage)
    17  }
    18  
    19  func Flagfn1(name, usage string, f func(string)) {
    20  	flag.Var(fn1(f), name, usage)
    21  }
    22  
    23  func Flagprint(fd int) {
    24  	if fd == 1 {
    25  		flag.CommandLine.SetOutput(os.Stdout)
    26  	}
    27  	flag.PrintDefaults()
    28  }
    29  
    30  func Flagparse(usage func()) {
    31  	flag.Usage = usage
    32  	flag.Parse()
    33  }
    34  
    35  func AddVersionFlag() {
    36  	flag.Var(versionFlag{}, "V", "print version and exit")
    37  }
    38  
    39  var buildID string // filled in by linker
    40  
    41  type versionFlag struct{}
    42  
    43  func (versionFlag) IsBoolFlag() bool { return true }
    44  func (versionFlag) Get() interface{} { return nil }
    45  func (versionFlag) String() string   { return "" }
    46  func (versionFlag) Set(s string) error {
    47  	name := os.Args[0]
    48  	name = name[strings.LastIndex(name, `/`)+1:]
    49  	name = name[strings.LastIndex(name, `\`)+1:]
    50  	p := Expstring()
    51  	if p == DefaultExpstring() {
    52  		p = ""
    53  	}
    54  	sep := ""
    55  	if p != "" {
    56  		sep = " "
    57  	}
    58  
    59  	// The go command invokes -V=full to get a unique identifier
    60  	// for this tool. It is assumed that the release version is sufficient
    61  	// for releases, but during development we include the full
    62  	// build ID of the binary, so that if the compiler is changed and
    63  	// rebuilt, we notice and rebuild all packages.
    64  	if s == "full" && strings.HasPrefix(Version, "devel") {
    65  		p += " buildID=" + buildID
    66  	}
    67  	fmt.Printf("%s version %s%s%s\n", name, Version, sep, p)
    68  	os.Exit(0)
    69  	return nil
    70  }
    71  
    72  // count is a flag.Value that is like a flag.Bool and a flag.Int.
    73  // If used as -name, it increments the count, but -name=x sets the count.
    74  // Used for verbose flag -v.
    75  type count int
    76  
    77  func (c *count) String() string {
    78  	return fmt.Sprint(int(*c))
    79  }
    80  
    81  func (c *count) Set(s string) error {
    82  	switch s {
    83  	case "true":
    84  		*c++
    85  	case "false":
    86  		*c = 0
    87  	default:
    88  		n, err := strconv.Atoi(s)
    89  		if err != nil {
    90  			return fmt.Errorf("invalid count %q", s)
    91  		}
    92  		*c = count(n)
    93  	}
    94  	return nil
    95  }
    96  
    97  func (c *count) IsBoolFlag() bool {
    98  	return true
    99  }
   100  
   101  type fn0 func()
   102  
   103  func (f fn0) Set(s string) error {
   104  	f()
   105  	return nil
   106  }
   107  
   108  func (f fn0) Get() interface{} { return nil }
   109  
   110  func (f fn0) String() string { return "" }
   111  
   112  func (f fn0) IsBoolFlag() bool {
   113  	return true
   114  }
   115  
   116  type fn1 func(string)
   117  
   118  func (f fn1) Set(s string) error {
   119  	f(s)
   120  	return nil
   121  }
   122  
   123  func (f fn1) String() string { return "" }