github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/cmd/dist/main.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 file.
     4  
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  	"strconv"
    12  )
    13  
    14  // cmdtab records the available commands.
    15  var cmdtab = []struct {
    16  	name string
    17  	f    func()
    18  }{
    19  	{"banner", cmdbanner},
    20  	{"bootstrap", cmdbootstrap},
    21  	{"clean", cmdclean},
    22  	{"env", cmdenv},
    23  	{"install", cmdinstall},
    24  	{"test", cmdtest},
    25  	{"version", cmdversion},
    26  }
    27  
    28  // The OS-specific main calls into the portable code here.
    29  func xmain() {
    30  	if len(os.Args) < 2 {
    31  		usage()
    32  	}
    33  	cmd := os.Args[1]
    34  	os.Args = os.Args[1:] // for flag parsing during cmd
    35  	for _, ct := range cmdtab {
    36  		if ct.name == cmd {
    37  			flag.Usage = func() {
    38  				fmt.Fprintf(os.Stderr, "usage: go tool dist %s [options]\n", cmd)
    39  				flag.PrintDefaults()
    40  				os.Exit(2)
    41  			}
    42  			ct.f()
    43  			return
    44  		}
    45  	}
    46  
    47  	xprintf("unknown command %s\n", cmd)
    48  	usage()
    49  }
    50  
    51  func xflagparse(maxargs int) {
    52  	flag.Var((*count)(&vflag), "v", "verbosity")
    53  	flag.Parse()
    54  	if maxargs >= 0 && flag.NArg() > maxargs {
    55  		flag.Usage()
    56  	}
    57  }
    58  
    59  // count is a flag.Value that is like a flag.Bool and a flag.Int.
    60  // If used as -name, it increments the count, but -name=x sets the count.
    61  // Used for verbose flag -v.
    62  type count int
    63  
    64  func (c *count) String() string {
    65  	return fmt.Sprint(int(*c))
    66  }
    67  
    68  func (c *count) Set(s string) error {
    69  	switch s {
    70  	case "true":
    71  		*c++
    72  	case "false":
    73  		*c = 0
    74  	default:
    75  		n, err := strconv.Atoi(s)
    76  		if err != nil {
    77  			return fmt.Errorf("invalid count %q", s)
    78  		}
    79  		*c = count(n)
    80  	}
    81  	return nil
    82  }
    83  
    84  func (c *count) IsBoolFlag() bool {
    85  	return true
    86  }