// Parse commandline arguments into getopt object func getOptions() map[string]getopt.OptionValue { optionDefinition := getopt.Options{ Description: "Beartrap v0.3 by Chris Benedict <*****@*****.**>", Definitions: getopt.Definitions{ {"config|c|BEARTRAP_CONFIG", "configuration file", getopt.Required, ""}, }, } options, _, _, err := optionDefinition.ParseCommandLine() help, wantsHelp := options["help"] if err != nil || wantsHelp { exitCode := 0 switch { case wantsHelp && help.String == "usage": fmt.Print(optionDefinition.Usage()) case wantsHelp && help.String == "help": fmt.Print(optionDefinition.Help()) default: fmt.Println("**** Error: ", err.Error(), "\n", optionDefinition.Help()) exitCode = err.ErrorCode } os.Exit(exitCode) } return options }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) optionDefinition := getopt.Options{ "description", getopt.Definitions{ {"config|c", "config file", getopt.IsConfigFile | getopt.ExampleIsDefault, "conf/server.conf"}, {"version|v", "show version", getopt.Optional | getopt.Flag, nil}, }, } options, _, _, e := optionDefinition.ParseCommandLine() help, wantsHelp := options["help"] if e != nil || wantsHelp { exit_code := 0 switch { case wantsHelp && help.String == "usage": fmt.Print(optionDefinition.Usage()) case wantsHelp && help.String == "help": fmt.Print(optionDefinition.Help()) default: fmt.Println("**** Error: ", e.Error(), "\n", optionDefinition.Help()) exit_code = e.ErrorCode } os.Exit(exit_code) } version, showVersion := options["version"] if showVersion && version.Bool { fmt.Printf("server version %s\n%s\n", _VERSION_, _BUILDDATE_) os.Exit(0) } cfg, err := config.Load(options["config"].String) if err != nil { log.Println("load config falied:", err) os.Exit(0) } if app, err := server.NewApp(cfg); err != nil { log.Println(err.Error()) } else { sc := make(chan os.Signal, 1) signal.Notify(sc, os.Kill, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) go app.Run() <-sc app.Close() } }
func (su *Supervise) ParseArgs() { var optDef = getopt.Options{ "supervise", getopt.Definitions{ {"status-dir|d|SU_STATUS_DIR", "status directory", getopt.Required, su.rundir}, {"cmd|c|SU_CMD", "command line", getopt.Required, "/sbin/hello -d /etc/hello.conf"}, {"control|s", "operation", getopt.Optional | getopt.ExampleIsDefault, "start"}, {"daemon|D", "run as daemon", getopt.Flag, false}, }, } var opts, _, _, err = optDef.ParseCommandLine() var help, ok = opts["help"] if err != nil || ok { switch help.String { case "help": fmt.Print(optDef.Help()) case "usage": fmt.Print(optDef.Usage()) default: fmt.Print(optDef.Help()) } os.Exit(SU_OK) } for k, v := range opts { switch k { case "status-dir": su.rundir = v.String os.Setenv("SU_STATUS_DIR", v.String) case "cmd": su.cmd = v.String os.Setenv("SU_CMD", v.String) case "control": su.action = v.String case "daemon": su.daemon = v.Bool } } }
func main() { fmt.Println("stone") optionDefinition := getopt.Options{ "Stone", getopt.Definitions{ {"port|p", "port", getopt.Optional | getopt.ExampleIsDefault, 1234}, }, } options, _, _, e := optionDefinition.ParseCommandLine() help, wantsHelp := options["help"] if e != nil || wantsHelp { exit_code := 0 switch { case wantsHelp && help.String == "usage": fmt.Print(optionDefinition.Usage()) case wantsHelp && help.String == "help": fmt.Print(optionDefinition.Help()) default: fmt.Println("Error: ", e.Error(), "\n", optionDefinition.Help()) exit_code = e.ErrorCode } os.Exit(exit_code) } node := new(Node) rpc.Register(node) rpc.HandleHTTP() l, err := net.Listen("tcp", fmt.Sprintf(":%d", options["port"].Int)) if err != nil { log.Fatal("Network listen error: ", e) } http.Serve(l, nil) }