func init() { runtime.GOMAXPROCS(runtime.NumCPU()) flag.Int("vv", 0, "vv") var fs = flag.NewFlagSet("deblocus", flag.ErrorHandling(0xffff)) var null, _ = os.Open(os.DevNull) defer null.Close() fs.SetOutput(null) var v int fs.IntVar(&v, "vv", 0, "log verbose") var deblocusArgs []string deblocusArgs = append(deblocusArgs, os.Args[1:]...) fs.Parse(deblocusArgs) log.SetLogOutput("") log.SetLogVerbose(v) cltAddr += strconv.FormatInt(randomRange(1, 1<<13)+3e4, 10) svrAddr += strconv.FormatInt(randomRange(1, 1<<13)+3e4, 10) dstAddr += strconv.FormatInt(randomRange(1, 1<<13)+3e4, 10) fmt.Println("=== deblocus TEST ===") printArgLine("logV", v) printArgLine("cltAddr", cltAddr) printArgLine("svrAddr", svrAddr) printArgLine("dstAddr", dstAddr) fmt.Println("\n") }
func initFlagSets(_flagsets interface{}) { flagsets := _flagsets.([]interface{}) for i, _flagset := range flagsets { flagset := _flagset.(map[string]interface{}) errorHandling := flagset["errorHandling"].(float64) name := flagset["name"].(string) if errorHandling < 0 || errorHandling > 2 { log.Fatalf("Unable to parse flagset at index \"%d\"\n", i) } if len(name) == 0 { log.Fatalf("Unable to parse flagset at index %d\n", i) } newFlagSet := flag.NewFlagSet(name, flag.ErrorHandling(errorHandling)) flagvars.m[name] = make(map[string]interface{}) err := initFlags(flagset["flags"], name, newFlagSet) if err != nil { log.Fatalf("Unable to parse flagset at index %d: %s\n", i, err) } if index := getArgIndex(name); index > -1 { newFlagSet.Parse(os.Args[index:]) } } }
// NewCmdVar defines a cmd with specified name, and explain string. // The argument cmd points to a Cmd variable in which to store the flags of the cmd. func (c *CmdSet) NewCmdVar(cmd *Cmd, name string, explain string) { cmd.Name = name cmd.Explain = explain cmd.Init(name, flag.ErrorHandling(c.errorHandling)) cmd.SetOutput(c.output) c.cmds[name] = cmd if len(name) > c.maxCmdLen { c.maxCmdLen = len(name) } }
func GetModule(key string) (Module, bool) { lock.Lock() defer lock.Unlock() cf, has := modules[key] if has { v, p := cf() policies[key] = flag.ErrorHandling(p) return v, true } return nil, false }
func TestBadFlags(t *testing.T) { // Test that this line *does* break: teststring := []string{"-bad"} const ContinueOnError = true devnull, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0700) flag.CommandLine.SetOutput(devnull) flag.Usage = func() {} flag.CommandLine.Init("testflags", flag.ErrorHandling(0)) e := flag.CommandLine.Parse(teststring) if e == nil { fmt.Println("\tExpected Error. Got nil.") t.FailNow() } else if e.Error() == "flag provided but not defined: -bad" { // } else { fmt.Println("\tExpected a different error:", e) } return }
// Test that this line does not break: // 'cosgo -gpg /etc/aerth.asc -fastcgi -log cosgo.log -port 8089' // Lots of flags: bind config debug fastcgi gpg log mbox new nolog port quiet sg title to // No more breaking crontabs on cosgo upgrades! // These flag tests need to be at the bottom of the test suite func TestFlags(t *testing.T) { // This causes some races so... if !*debug { return } const PanicOnError = true devnull, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0700) flag.CommandLine.SetOutput(devnull) flag.CommandLine.Init("testflags", flag.ErrorHandling(2)) flag.Usage = func() {} orig := os.Args os.Args = []string{"cosgo", "-gpg", "/etc/aerth.asc", "-fastcgi", "-log", "cosgo.log", "-port", "8089"} flag.Parse() os.Args = []string{"cosgo", "-mbox", "test.mbox", "-bind", "0.0.0.0", "-debug"} flag.Parse() os.Args = []string{"cosgo", "-title", "Test Page", "-fastcgi", "-log", "cosgo.log", "-port", "8089"} flag.Parse() os.Args = []string{"cosgo", "-config", "/tmp/config.cosgo", "-nolog", "-sg", "sgkey123", "-to", "*****@*****.**"} flag.Parse() os.Args = orig flag.Parse() return }