github.com/goplus/igop@v0.17.0/cmd/igop/main.go (about)

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