github.com/goplus/llgo@v0.8.3/cmd/llgo/llgo.go (about)

     1  /*
     2   * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
     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/qiniu/x/log"
    26  
    27  	"github.com/goplus/llgo/cmd/internal/base"
    28  	"github.com/goplus/llgo/cmd/internal/build"
    29  	"github.com/goplus/llgo/cmd/internal/clean"
    30  	"github.com/goplus/llgo/cmd/internal/help"
    31  	"github.com/goplus/llgo/cmd/internal/install"
    32  	"github.com/goplus/llgo/cmd/internal/run"
    33  )
    34  
    35  func mainUsage() {
    36  	help.PrintUsage(os.Stderr, base.Llgo)
    37  	os.Exit(2)
    38  }
    39  
    40  func init() {
    41  	flag.Usage = mainUsage
    42  	base.Llgo.Commands = []*base.Command{
    43  		build.Cmd,
    44  		install.Cmd,
    45  		run.Cmd,
    46  		clean.Cmd,
    47  	}
    48  }
    49  
    50  func main() {
    51  	flag.Parse()
    52  	args := flag.Args()
    53  	if len(args) < 1 {
    54  		flag.Usage()
    55  	}
    56  	log.SetFlags(log.Ldefault &^ log.LstdFlags)
    57  
    58  	base.CmdName = args[0] // for error messages
    59  	if args[0] == "help" {
    60  		help.Help(os.Stderr, args[1:])
    61  		return
    62  	}
    63  
    64  BigCmdLoop:
    65  	for bigCmd := base.Llgo; ; {
    66  		for _, cmd := range bigCmd.Commands {
    67  			if cmd.Name() != args[0] {
    68  				continue
    69  			}
    70  			args = args[1:]
    71  			if len(cmd.Commands) > 0 {
    72  				bigCmd = cmd
    73  				if len(args) == 0 {
    74  					help.PrintUsage(os.Stderr, bigCmd)
    75  					os.Exit(2)
    76  				}
    77  				if args[0] == "help" {
    78  					help.Help(os.Stderr, append(strings.Split(base.CmdName, " "), args[1:]...))
    79  					return
    80  				}
    81  				base.CmdName += " " + args[0]
    82  				continue BigCmdLoop
    83  			}
    84  			if !cmd.Runnable() {
    85  				continue
    86  			}
    87  			cmd.Run(cmd, args)
    88  			return
    89  		}
    90  		helpArg := ""
    91  		if i := strings.LastIndex(base.CmdName, " "); i >= 0 {
    92  			helpArg = " " + base.CmdName[:i]
    93  		}
    94  		fmt.Fprintf(os.Stderr, "llgo %s: unknown command\nRun 'llgo help%s' for usage.\n", base.CmdName, helpArg)
    95  		os.Exit(2)
    96  	}
    97  }