/
command.go
60 lines (46 loc) · 1.08 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"flag"
"fmt"
"os"
"strings"
)
// A Command is an implementation of a goats command like goats build or goats fix.
type Command struct {
// Run runs the command.
// The args are the arguments after the command name.
Run func(cmd *Command, args []string)
Name string
// UsageLine is the one-line usage message.
// The first word in the line is taken to be the command name.
UsageLine string
// Short is the short description shown in the 'go help' output.
Short string
// Long is the long message shown in the 'go help <this-command>' output.
Long string
// Flag is a set of flags specific to this command.
Flag flag.FlagSet
}
func (c *Command) Usage() {
fmt.Printf(strings.TrimSpace(c.Short))
fmt.Printf(`
Usage:
%s
`, c.UsageLine)
fmt.Printf("%s\n\n", strings.TrimSpace(c.Long))
c.Flag.PrintDefaults()
os.Exit(2)
}
func (c *Command) Exec(args []string) {
c.Flag.Parse(args)
defer func() {
if r := recover(); r != nil {
if _, ok := r.(fatal); ok {
os.Exit(1)
}
panic(r)
}
}()
c.Run(c, c.Flag.Args())
}
type fatal struct{}