github.com/xraypb/xray-core@v1.6.6/main/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"os"
     6  
     7  	"github.com/xraypb/xray-core/main/commands/base"
     8  	_ "github.com/xraypb/xray-core/main/distro/all"
     9  )
    10  
    11  func main() {
    12  	os.Args = getArgsV4Compatible()
    13  
    14  	base.RootCommand.Long = "Xray is a platform for building proxies."
    15  	base.RootCommand.Commands = append(
    16  		[]*base.Command{
    17  			cmdRun,
    18  			cmdVersion,
    19  		},
    20  		base.RootCommand.Commands...,
    21  	)
    22  	base.Execute()
    23  }
    24  
    25  func getArgsV4Compatible() []string {
    26  	if len(os.Args) == 1 {
    27  		return []string{os.Args[0], "run"}
    28  	}
    29  	if os.Args[1][0] != '-' {
    30  		return os.Args
    31  	}
    32  	version := false
    33  	fs := flag.NewFlagSet("", flag.ContinueOnError)
    34  	fs.BoolVar(&version, "version", false, "")
    35  	// parse silently, no usage, no error output
    36  	fs.Usage = func() {}
    37  	fs.SetOutput(&null{})
    38  	err := fs.Parse(os.Args[1:])
    39  	if err == flag.ErrHelp {
    40  		// fmt.Println("DEPRECATED: -h, WILL BE REMOVED IN V5.")
    41  		// fmt.Println("PLEASE USE: xray help")
    42  		// fmt.Println()
    43  		return []string{os.Args[0], "help"}
    44  	}
    45  	if version {
    46  		// fmt.Println("DEPRECATED: -version, WILL BE REMOVED IN V5.")
    47  		// fmt.Println("PLEASE USE: xray version")
    48  		// fmt.Println()
    49  		return []string{os.Args[0], "version"}
    50  	}
    51  	// fmt.Println("COMPATIBLE MODE, DEPRECATED.")
    52  	// fmt.Println("PLEASE USE: xray run [arguments] INSTEAD.")
    53  	// fmt.Println()
    54  	return append([]string{os.Args[0], "run"}, os.Args[1:]...)
    55  }
    56  
    57  type null struct{}
    58  
    59  func (n *null) Write(p []byte) (int, error) {
    60  	return len(p), nil
    61  }