github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/mitchellh/cli"
     8  )
     9  
    10  func main() {
    11  	os.Exit(Run(os.Args[1:]))
    12  }
    13  
    14  func Run(args []string) int {
    15  	return RunCustom(args, Commands(nil))
    16  }
    17  
    18  func RunCustom(args []string, commands map[string]cli.CommandFactory) int {
    19  	// Get the command line args. We shortcut "--version" and "-v" to
    20  	// just show the version.
    21  	for _, arg := range args {
    22  		if arg == "-v" || arg == "-version" || arg == "--version" {
    23  			newArgs := make([]string, len(args)+1)
    24  			newArgs[0] = "version"
    25  			copy(newArgs[1:], args)
    26  			args = newArgs
    27  			break
    28  		}
    29  	}
    30  
    31  	// Build the commands to include in the help now.
    32  	commandsInclude := make([]string, 0, len(commands))
    33  	for k, _ := range commands {
    34  		switch k {
    35  		case "spawn-daemon":
    36  		default:
    37  			commandsInclude = append(commandsInclude, k)
    38  		}
    39  	}
    40  
    41  	cli := &cli.CLI{
    42  		Args:     args,
    43  		Commands: commands,
    44  		HelpFunc: cli.FilteredHelpFunc(commandsInclude, cli.BasicHelpFunc("nomad")),
    45  	}
    46  
    47  	exitCode, err := cli.Run()
    48  	if err != nil {
    49  		fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
    50  		return 1
    51  	}
    52  
    53  	return exitCode
    54  }