Home | History | Annotate | Download | only in obj
      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 newIntValue(val int32, p *int32) *int32Value {
     84 	*p = val
     85 	return (*int32Value)(p)
     86 }
     87 
     88 func (i *int32Value) Set(s string) error {
     89 	v, err := strconv.ParseInt(s, 0, 64)
     90 	*i = int32Value(v)
     91 	return err
     92 }
     93 
     94 func (i *int32Value) Get() interface{} { return int32(*i) }
     95 
     96 func (i *int32Value) String() string { return fmt.Sprint(*i) }
     97 
     98 type fn0 func()
     99 
    100 func (f fn0) Set(s string) error {
    101 	f()
    102 	return nil
    103 }
    104 
    105 func (f fn0) Get() interface{} { return nil }
    106 
    107 func (f fn0) String() string { return "" }
    108 
    109 func (f fn0) IsBoolFlag() bool {
    110 	return true
    111 }
    112 
    113 type fn1 func(string)
    114 
    115 func (f fn1) Set(s string) error {
    116 	f(s)
    117 	return nil
    118 }
    119 
    120 func (f fn1) String() string { return "" }
    121