-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
54 lines (45 loc) · 775 Bytes
/
main.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
package main
import (
"flag"
"fmt"
"os"
)
type Command struct {
Name string
Usage string
ShortHelp string
LongHelp string
Run func(args []string)
Flag flag.FlagSet
}
var commands = []*Command{
eval,
serve,
}
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) < 1 {
usage()
return
}
for _, cmd := range commands {
if cmd.Name == args[0] && cmd.Run != nil {
cmd.Flag.Parse(args[1:])
cmd_args := cmd.Flag.Args()
cmd.Run(cmd_args)
os.Exit(0)
}
}
fmt.Fprintf(os.Stderr, "Unknown subcommand %q\n", args[0])
}
func usage() {
fmt.Printf("usage: %s\n\n", os.Args[0])
for _, cmd := range commands {
if cmd.Usage != "" {
fmt.Printf(" %s\n", cmd.Usage)
}
}
fmt.Printf("\n")
}