github.com/FenixAra/go@v0.0.0-20170127160404-96ea0918e670/src/cmd/internal/obj/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 obj
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  	"strconv"
    12  )
    13  
    14  func Flagfn2(string, string, func(string, string)) { panic("flag") }
    15  
    16  func Flagcount(name, usage string, val *int) {
    17  	flag.Var((*count)(val), name, usage)
    18  }
    19  
    20  func Flagint32(name, usage string, val *int32) {
    21  	flag.Var((*int32Value)(val), name, usage)
    22  }
    23  
    24  func Flagint64(name, usage string, val *int64) {
    25  	flag.Int64Var(val, name, *val, usage)
    26  }
    27  
    28  func Flagstr(name, usage string, val *string) {
    29  	flag.StringVar(val, name, *val, usage)
    30  }
    31  
    32  func Flagfn0(name, usage string, f func()) {
    33  	flag.Var(fn0(f), name, usage)
    34  }
    35  
    36  func Flagfn1(name, usage string, f func(string)) {
    37  	flag.Var(fn1(f), name, usage)
    38  }
    39  
    40  func Flagprint(fd int) {
    41  	if fd == 1 {
    42  		flag.CommandLine.SetOutput(os.Stdout)
    43  	}
    44  	flag.PrintDefaults()
    45  }
    46  
    47  func Flagparse(usage func()) {
    48  	flag.Usage = usage
    49  	flag.Parse()
    50  }
    51  
    52  // count is a flag.Value that is like a flag.Bool and a flag.Int.
    53  // If used as -name, it increments the count, but -name=x sets the count.
    54  // Used for verbose flag -v.
    55  type count int
    56  
    57  func (c *count) String() string {
    58  	return fmt.Sprint(int(*c))
    59  }
    60  
    61  func (c *count) Set(s string) error {
    62  	switch s {
    63  	case "true":
    64  		*c++
    65  	case "false":
    66  		*c = 0
    67  	default:
    68  		n, err := strconv.Atoi(s)
    69  		if err != nil {
    70  			return fmt.Errorf("invalid count %q", s)
    71  		}
    72  		*c = count(n)
    73  	}
    74  	return nil
    75  }
    76  
    77  func (c *count) IsBoolFlag() bool {
    78  	return true
    79  }
    80  
    81  type int32Value int32
    82  
    83  func (i *int32Value) Set(s string) error {
    84  	v, err := strconv.ParseInt(s, 0, 64)
    85  	*i = int32Value(v)
    86  	return err
    87  }
    88  
    89  func (i *int32Value) Get() interface{} { return int32(*i) }
    90  
    91  func (i *int32Value) String() string { return fmt.Sprint(*i) }
    92  
    93  type fn0 func()
    94  
    95  func (f fn0) Set(s string) error {
    96  	f()
    97  	return nil
    98  }
    99  
   100  func (f fn0) Get() interface{} { return nil }
   101  
   102  func (f fn0) String() string { return "" }
   103  
   104  func (f fn0) IsBoolFlag() bool {
   105  	return true
   106  }
   107  
   108  type fn1 func(string)
   109  
   110  func (f fn1) Set(s string) error {
   111  	f(s)
   112  	return nil
   113  }
   114  
   115  func (f fn1) String() string { return "" }