github.com/goplus/gossa@v0.3.25/cmd/gossa/main.go (about) 1 /* 2 Copyright 2021 The GoPlus Authors (goplus.org) 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "flag" 21 "fmt" 22 "os" 23 "strings" 24 25 "github.com/goplus/gossa/cmd/internal/base" 26 "github.com/goplus/gossa/cmd/internal/help" 27 "github.com/goplus/gossa/cmd/internal/repl" 28 "github.com/goplus/gossa/cmd/internal/run" 29 "github.com/goplus/gossa/cmd/internal/test" 30 31 _ "github.com/goplus/gossa/pkg" 32 _ "github.com/goplus/reflectx/icall/icall8192" 33 ) 34 35 func mainUsage() { 36 help.PrintUsage(os.Stderr, base.Gossa) 37 os.Exit(2) 38 } 39 40 func init() { 41 base.Usage = mainUsage 42 base.Gossa.Commands = []*base.Command{ 43 run.Cmd, 44 test.Cmd, 45 repl.Cmd, 46 } 47 } 48 49 func main() { 50 flag.Parse() 51 args := flag.Args() 52 if len(args) < 1 { 53 //base.Usage() 54 args = []string{"repl"} 55 } 56 57 base.CmdName = args[0] // for error messages 58 if args[0] == "help" { 59 help.Help(os.Stderr, args[1:]) 60 return 61 } 62 63 BigCmdLoop: 64 for bigCmd := base.Gossa; ; { 65 for _, cmd := range bigCmd.Commands { 66 if cmd.Name() != args[0] { 67 continue 68 } 69 args = args[1:] 70 if len(cmd.Commands) > 0 { 71 bigCmd = cmd 72 if len(args) == 0 { 73 help.PrintUsage(os.Stderr, bigCmd) 74 os.Exit(2) 75 } 76 if args[0] == "help" { 77 help.Help(os.Stderr, append(strings.Split(base.CmdName, " "), args[1:]...)) 78 return 79 } 80 base.CmdName += " " + args[0] 81 continue BigCmdLoop 82 } 83 if !cmd.Runnable() { 84 continue 85 } 86 cmd.Run(cmd, args) 87 return 88 } 89 helpArg := "" 90 if i := strings.LastIndex(base.CmdName, " "); i >= 0 { 91 helpArg = " " + base.CmdName[:i] 92 } 93 fmt.Fprintf(os.Stderr, "gossa %s: unknown command\nRun 'gossa help%s' for usage.\n", base.CmdName, helpArg) 94 os.Exit(2) 95 } 96 }