-
Notifications
You must be signed in to change notification settings - Fork 546
/
main.go
86 lines (78 loc) · 1.68 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/goplus/gop/cmd/internal/base"
"github.com/goplus/gop/cmd/internal/build"
"github.com/goplus/gop/cmd/internal/export"
"github.com/goplus/gop/cmd/internal/gengo"
"github.com/goplus/gop/cmd/internal/gopfmt"
"github.com/goplus/gop/cmd/internal/help"
"github.com/goplus/gop/cmd/internal/install"
"github.com/goplus/gop/cmd/internal/repl"
"github.com/goplus/gop/cmd/internal/run"
_ "github.com/goplus/gop/lib"
)
func mainUsage() {
help.PrintUsage(os.Stderr, base.Gop)
os.Exit(2)
}
func init() {
base.Usage = mainUsage
base.Gop.Commands = []*base.Command{
run.Cmd,
gengo.Cmd,
gopfmt.Cmd,
export.Cmd,
repl.Cmd,
install.Cmd,
build.Cmd,
}
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
base.Usage()
}
base.CmdName = args[0] // for error messages
if args[0] == "help" {
help.Help(os.Stderr, args[1:])
return
}
BigCmdLoop:
for bigCmd := base.Gop; ; {
for _, cmd := range bigCmd.Commands {
if cmd.Name() != args[0] {
continue
}
args = args[1:]
if len(cmd.Commands) > 0 {
bigCmd = cmd
if len(args) == 0 {
help.PrintUsage(os.Stderr, bigCmd)
os.Exit(2)
}
if args[0] == "help" {
help.Help(os.Stderr, append(strings.Split(base.CmdName, " "), args[1:]...))
return
}
base.CmdName += " " + args[0]
continue BigCmdLoop
}
if !cmd.Runnable() {
continue
}
cmd.Run(cmd, args)
return
}
helpArg := ""
if i := strings.LastIndex(base.CmdName, " "); i >= 0 {
helpArg = " " + base.CmdName[:i]
}
fmt.Fprintf(os.Stderr, "gop %s: unknown command\nRun 'gop help%s' for usage.\n", base.CmdName, helpArg)
os.Exit(2)
}
}