github.com/goplus/igop@v0.25.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/pkg"
    33  	_ "github.com/goplus/reflectx/icall/icall8192"
    34  )
    35  
    36  func mainUsage() {
    37  	help.PrintUsage(os.Stderr, base.Igop)
    38  	os.Exit(2)
    39  }
    40  
    41  func init() {
    42  	base.Usage = mainUsage
    43  	base.Igop.Commands = []*base.Command{
    44  		run.Cmd,
    45  		build.Cmd,
    46  		test.Cmd,
    47  		repl.Cmd,
    48  		version.Cmd,
    49  		export.Cmd,
    50  	}
    51  }
    52  
    53  func main() {
    54  	flag.Parse()
    55  	args := flag.Args()
    56  	if len(args) < 1 {
    57  		//base.Usage()
    58  		args = []string{"repl"}
    59  	}
    60  
    61  	base.CmdName = args[0] // for error messages
    62  	if args[0] == "help" {
    63  		help.Help(os.Stderr, args[1:])
    64  		return
    65  	}
    66  
    67  BigCmdLoop:
    68  	for bigCmd := base.Igop; ; {
    69  		for _, cmd := range bigCmd.Commands {
    70  			if cmd.Name() != args[0] {
    71  				continue
    72  			}
    73  			args = args[1:]
    74  			if len(cmd.Commands) > 0 {
    75  				bigCmd = cmd
    76  				if len(args) == 0 {
    77  					help.PrintUsage(os.Stderr, bigCmd)
    78  					os.Exit(2)
    79  				}
    80  				if args[0] == "help" {
    81  					help.Help(os.Stderr, append(strings.Split(base.CmdName, " "), args[1:]...))
    82  					return
    83  				}
    84  				base.CmdName += " " + args[0]
    85  				continue BigCmdLoop
    86  			}
    87  			if !cmd.Runnable() {
    88  				continue
    89  			}
    90  			cmd.Run(cmd, args)
    91  			return
    92  		}
    93  		helpArg := ""
    94  		if i := strings.LastIndex(base.CmdName, " "); i >= 0 {
    95  			helpArg = " " + base.CmdName[:i]
    96  		}
    97  		fmt.Fprintf(os.Stderr, "igop %s: unknown command\nRun 'igop help%s' for usage.\n", base.CmdName, helpArg)
    98  		os.Exit(2)
    99  	}
   100  }