func main() { flag.Parse() finished := make(chan bool) joblist := newJoblist(finished) useCmdFile := false useCmdPort := false markFlags := func(f *flag.Flag) { if f.Name == "cmdport" { useCmdPort = true } else if f.Name == "cmdfile" { useCmdFile = true } } flag.Visit(markFlags) startWorkers(joblist) if useCmdFile { postCommandsFromFile(joblist, useCmdPort) } if !useCmdFile || useCmdPort { go startAdders(joblist) } <-finished logfile.Printf("%d jobs succeeded\n", joblist.nok) logfile.Printf("%d jobs failed\n", joblist.nfail) logfile.Printf("%d jobs completed\n", joblist.nok+joblist.nfail) }
func Parse() { if *configFile == "" { return } config := readConfig() explicit := make([]*flag.Flag, 0) all := make([]*flag.Flag, 0) flag.Visit(func(f *flag.Flag) { explicit = append(explicit, f) }) flag.VisitAll(func(f *flag.Flag) { all = append(all, f) if !contains(explicit, f) { val := config[f.Name] if val != "" { err := f.Value.Set(val) if err != nil { log.Fatalf("Failed to set flag %s with value %s", f.Name, val) } } } }) Outer: for name, val := range config { for _, f := range all { if f.Name == name { continue Outer } } log.Fatalf("Unknown flag %s=%s in config file.", name, val) } }
// Save set options to a configuration file. If categories are given, only those // options in one of the categories is saved. func Save(path, comment string, category ...string) error { file, err := util.CreatePath(path, 0777, 0777) if err != nil { return err } defer file.Close() set := make(map[string]bool) flag.Visit(func(f *flag.Flag) { set[f.Name] = true }) for _, s := range strings.Split(comment, "\n") { fmt.Fprintf(file, "# %s\n", s) } fmt.Fprintf(file, "# Generated on %s\n", time.Now()) for _, opt := range Options { f := flag.Lookup(opt.Name) if !opt.Relevant(category...) || f == nil { continue } fmt.Fprintf(file, "\n# %s\n", opt.Help) if set[opt.Name] { fmt.Fprintf(file, "%s = %v\n", opt.Name, f.Value) } else { fmt.Fprintf(file, "# %s = %v\n", opt.Name, f.Value) } } return nil }
func parse() (err error) { // Record which flags were set by command line args so that we don't overwrite them. set := make(map[*flag.Flag]bool, 0) flag.Visit(func(f *flag.Flag) { set[f] = true }) flag.VisitAll(func(f *flag.Flag) { if !set[f] && err == nil { r := strings.NewReplacer(".", "_", "-", "_") name := r.Replace(f.Name) if UseUpperCaseFlagNames { name = strings.ToUpper(name) } val := os.Getenv(name) if val != "" { if seterr := f.Value.Set(val); seterr != nil { err = fmt.Errorf("Failed to set flag %s with value %s", f.Name, val) } } } }) return }
func getMissingFlags() []string { var ( set, missing []string found = false ) // Visit only the flags that have been set flag.Visit(func(f *flag.Flag) { set = append(set, f.Name) }) // Visit all the flags, even those not set flag.VisitAll(func(f *flag.Flag) { for _, v := range set { if v == f.Name { found = true break } } // If we don't find the flag in the slice of already set flags, we add it to the missing slice if !found { missing = append(missing, f.Name) } found = false }) return missing }
func configure(flags ServerConfig) { if flag.NArg() == 1 { fname := flag.Arg(0) f, err := os.Open(fname) if err != nil { log.Fatalf("Config reading error: %s\n", err.String()) } dec := json.NewDecoder(f) err = dec.Decode(&server) if err != nil { log.Fatalf("Config parsing error: %s\n", err.String()) } action = "serve" } flag.Visit(func(flag *flag.Flag) { switch flag.Name { case "version": action = "version" case "port": server.Port = flags.Port case "host": server.Host = flags.Host case "tls-cert": server.TLSCertFile = flags.TLSCertFile case "tls-key": server.TLSKeyFile = flags.TLSKeyFile case "log": server.Log = flags.Log } }) }
func loadConfig(nm string, flagSet *flag.FlagSet, isOverride bool) error { f, e := os.Open(nm) if nil != e { return fmt.Errorf("load config '%s' failed, %v", nm, e) } var res map[string]interface{} e = json.NewDecoder(f).Decode(&res) if nil != e { return fmt.Errorf("load config '%s' failed, %v", nm, e) } actual := map[string]string{} fn := func(f *flag.Flag) { actual[f.Name] = f.Name } if nil == flagSet { flag.Visit(fn) } else { flagSet.Visit(fn) } e = assignFlagSet("", res, flagSet, actual, isOverride) if nil != e { return fmt.Errorf("load config '%s' failed, %v", nm, e) } return nil }
// parse() iterates through the flags and populate the // values from environment variables if present func parse(prefix string) (errp error) { cmdline := make(map[string]bool) flag.Visit(func(f *flag.Flag) { cmdline[f.Name] = true }) flag.VisitAll(func(f *flag.Flag) { if _, ok := cmdline[f.Name]; !ok { val := os.Getenv(getEnvName(prefix, f.Name)) if val != "" { err := f.Value.Set(val) if err != nil { if errp != nil { errp = fmt.Errorf(errp.Error()+", value:\"%s\" for flag:\"%s\"", val, f.Name) } else { errp = fmt.Errorf(":envtoflag: error setting value:\"%s\" for flag:\"%s\"", val, f.Name) } } } } }) return }
func (flagConf *DBConf) FlagMerge(fileConf *DBConf) (*DBConf, error) { only := make(map[string]bool, 0) flag.Visit(func(f *flag.Flag) { only[f.Name] = true }) if only["map-seed-size"] { return flagConf, fmt.Errorf("The map seed size cannot be changed for " + "an existing database.") } if only["read-only"] { return flagConf, fmt.Errorf("The read-only setting cannot be changed " + "for an existing database.") } if !only["min-match-len"] { flagConf.MinMatchLen = fileConf.MinMatchLen } if !only["match-kmer-size"] { flagConf.MatchKmerSize = fileConf.MatchKmerSize } if !only["gapped-window-size"] { flagConf.GappedWindowSize = fileConf.GappedWindowSize } if !only["ungapped-window-size"] { flagConf.UngappedWindowSize = fileConf.UngappedWindowSize } if !only["ext-seq-id-threshold"] { flagConf.ExtSeqIdThreshold = fileConf.ExtSeqIdThreshold } if !only["match-seq-id-threshold"] { flagConf.MatchSeqIdThreshold = fileConf.MatchSeqIdThreshold } if !only["match-extend"] { flagConf.MatchExtend = fileConf.MatchExtend } if !only["ext-seed-size"] { flagConf.ExtSeedSize = fileConf.ExtSeedSize } if !only["low-complexity"] { flagConf.LowComplexity = fileConf.LowComplexity } if !only["seed-low-complexity"] { flagConf.SeedLowComplexity = fileConf.SeedLowComplexity } if !only["plain"] { flagConf.SavePlain = fileConf.SavePlain } if !only["read-only"] { flagConf.ReadOnly = fileConf.ReadOnly } if !only["makeblastdb"] { flagConf.BlastMakeBlastDB = fileConf.BlastMakeBlastDB } if !only["diamond"] { flagConf.Dmnd = fileConf.Dmnd } if !only["dbsize"] { flagConf.BlastDBSize = fileConf.BlastDBSize } return flagConf, nil }
// getTaskArgs returns the arguments to be passed to "gake/tasking". func getTaskArgs() []string { args := make([]string, 0) flag.Visit(func(f *flag.Flag) { isBoolean := false switch f.Name { case "c", "x", "keep": // Flags skipped return // Rewrite known flags to have "task" before them case "cpu", "parallel", "run", "short", "timeout", "v": f.Name = "task." + f.Name fallthrough case "task.short", "task.v": isBoolean = true } args = append(args, "-"+f.Name) if !isBoolean { args = append(args, f.Value.String()) } }) fargs := flag.Args() if len(fargs) > 1 { args = append(args, "-task.args") args = append(args, fargs[1:]...) } return args }
func init() { flag.Parse() // Create a map of pointers to all the current flags flags := map[string]*flag.Flag{} flag.VisitAll(func(f *flag.Flag) { flags[f.Name] = f }) // Remove the flags that were set on the command line flag.Visit(func(f *flag.Flag) { delete(flags, f.Name) }) // Now for the flags that weren't set on the cli, // Look at the env for 'PBOX_<uppercase flag name>' // If it exists, use it to set the corresponding flag. for _, f := range flags { var buffer bytes.Buffer buffer.WriteString("PBOX_") buffer.WriteString(strings.ToUpper(f.Name)) if os.Getenv(buffer.String()) != "" { f.Value.Set(os.Getenv(buffer.String())) } } }
func main() { flag.Usage = usage flag.Parse() proc := lines flag.Visit(func(f *flag.Flag) { if f.Name == "c" { proc = chars } }) if flag.NArg() == 0 { proc(os.Stdin, "<stdin>") } else { for _, name := range flag.Args() { f, err := os.Open(name) if err != nil { fmt.Fprintln(os.Stderr, "read:", err) os.Exit(2) } proc(f, name) f.Close() } } os.Exit(status) }
func run(inFile string, gpu int, webAddr string) { // overridden flags gpuFlag := fmt.Sprint(`-gpu=`, gpu) httpFlag := fmt.Sprint(`-http=`, webAddr) // pass through flags flags := []string{gpuFlag, httpFlag} flag.Visit(func(f *flag.Flag) { if f.Name != "gpu" && f.Name != "http" && f.Name != "failfast" { flags = append(flags, fmt.Sprintf("-%v=%v", f.Name, f.Value)) } }) flags = append(flags, inFile) cmd := exec.Command(os.Args[0], flags...) log.Println(os.Args[0], flags) err := cmd.Run() if err != nil { log.Println(inFile, err) exitStatus.set(1) numFailed.inc() if *flag_failfast { os.Exit(1) } } else { numOK.inc() } }
// ConfigureLogging applies the configuration in revel.Config to the glog flags. // Logger flags specified explicitly on the command line are not changed. func ConfigureLogging() { // Get the flags specified on the command line. specifiedFlags := make(map[string]struct{}) flag.Visit(func(f *flag.Flag) { specifiedFlags[f.Name] = struct{}{} }) // For each logger option in app.conf.. var err error for _, option := range Config.Options("log.") { val, _ := Config.String(option) switch flagname := option[len("log."):]; flagname { case "v", "vmodule", "logtostderr", "alsologtostderr", "stderrthreshold", "log_dir": // If it was specified on the command line, don't set it from app.conf if _, ok := specifiedFlags[flagname]; ok { continue } // Look up the flag and set it. // If it's log_dir, make it into an absolute path and creat it if necessary. if flagname == "log_dir" { if val, err = filepath.Abs(val); err != nil { glog.Fatalln("Failed to get absolute path to log_dir:", err) } os.MkdirAll(val, 0777) // Create the log dir if it doesn't already exist. } if err = flag.Set(flagname, val); err != nil { glog.Fatalf("Failed to set glog option for %s=%s: %s", flagname, val, err) } case "maxsize": if glog.MaxSize, err = humanize.ParseBytes(val); err != nil { glog.Fatalf("Failed to parse log.MaxSize=%s: %s", val, err) } } } }
// Parse parses the command-line flags from os.Args[1:] and sets the values in // this ConfigoSet. Then the configuration file is parsed and any item that // was not already set by the command line is set. Must be called after all // configuration options are defined and before conifguration options are // accessed by the program. func (c *ConfigoSet) Parse() (err error) { // Start by parsing the command line with the flag package and then set the // parsed values into the ConfigoSet. flag.Parse() flag.Visit(func(f *flag.Flag) { c.Set(f.Name, f.Value.String()) }) // Now parse the configuration file, but first create the config file if it // does not exist. If that's the case we're all done and we can return. if _, err = os.Stat(c.path); err != nil { if !os.IsNotExist(err) { return } c.parsed = true err = c.WriteDefaultConfig(c.path) return } // Parse the config file, but only set the options that didn't appear on // the command line. if !c.parsed { var content []byte content, err = ioutil.ReadFile(c.path) if err != nil { return } for i, line := range strings.Split(string(content), "\n") { line = strings.TrimSpace(line) if len(line) > 0 && !strings.HasPrefix(line, "#") { var name, value string fields := strings.SplitN(line, c.delimiter, 2) if len(fields) != 2 { errors.New(fmt.Sprintf("Invalid key%svalue pair in conifiguration file %s on line %d.\n", c.delimiter, c.path, i)) } name = strings.TrimSpace(fields[0]) value = strings.TrimSpace(fields[1]) // Check if the item was already set from the command line. if _, exists := c.actual[name]; !exists { // Is this even a valid config item? config := c.Lookup(name) if config == nil { panic(errors.New("unknown configuration item")) } c.Set(name, value) } } } c.parsed = true } return }
func WhoCame() []*flag.Flag { var res []*flag.Flag visit := func(f *flag.Flag) { res = append(res, f) } flag.Visit(visit) return res }
func main() { var err error flag.Parse() fmt.Println("conquest", "v"+conquest.VERSION, "\n") if _, err := os.Stat(configfile); os.IsNotExist(err) { fmt.Println(configfile, "file not found") os.Exit(1) } conq, err := conquest.RunScript(configfile) if err != nil { fmt.Println(err) os.Exit(1) } flag.Visit(func(f *flag.Flag) { switch f.Name { case "u": conq.TotalUsers = users case "t": var duration time.Duration duration, err = time.ParseDuration(timeout) if err == nil { conq.Duration = duration } case "s": conq.Sequential = sequential } }) if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println("performing transactions...\n") var fo *os.File if output == "" { fo = os.Stdout } else { fo, err = os.Create(output) if err != nil { fmt.Println(err) os.Exit(1) } } reporter := conquest.NewReporter(fo, verbose) err = conquest.Perform(conq, reporter) if err != nil { fmt.Println(err) os.Exit(1) } <-reporter.C.Done }
func (rcvr *Receiver) NewReceiver() { switch strings.ToLower(*msgType) { case "scm": rcvr.p = scm.NewParser(*symbolLength, *fastMag) case "idm": rcvr.p = idm.NewParser(*symbolLength, *fastMag) case "r900": rcvr.p = r900.NewParser(*symbolLength, *fastMag) default: log.Fatalf("Invalid message type: %q\n", *msgType) } if !*quiet { rcvr.p.Cfg().Log() } // Connect to rtl_tcp server. if err := rcvr.Connect(nil); err != nil { log.Fatal(err) } rcvr.HandleFlags() // Tell the user how many gain settings were reported by rtl_tcp. if !*quiet { log.Println("GainCount:", rcvr.SDR.Info.GainCount) } centerfreqFlagSet := false sampleRateFlagSet := false gainFlagSet := false flag.Visit(func(f *flag.Flag) { switch f.Name { case "centerfreq": centerfreqFlagSet = true case "samplerate": sampleRateFlagSet = true case "gainbyindex", "tunergainmode", "tunergain", "agcmode": gainFlagSet = true } }) // Set some parameters for listening. if centerfreqFlagSet { rcvr.SetCenterFreq(uint32(rcvr.Flags.CenterFreq)) } else { rcvr.SetCenterFreq(rcvr.p.Cfg().CenterFreq) } if !sampleRateFlagSet { rcvr.SetSampleRate(uint32(rcvr.p.Cfg().SampleRate)) } if !gainFlagSet { rcvr.SetGainMode(true) } return }
func flagIsSet(name string) bool { set := false flag.Visit(func(f *flag.Flag) { if f.Name == name { set = true } }) return set }
func hasflag(name string) bool { found := false flag.Visit(func(f *flag.Flag) { if f.Name == name { found = true } }) return found }
func main() { flag.Usage = usage flag.Parse() flag.Visit(func(f *flag.Flag) { switch f.Name { case "C": setLogLevel(*level) } }) var err error logger, err = syslog.New(0, "") ck(err) if *kern { logger.Notice("KLOGD: started with Kernel ring buffer as log source\n") kctl(1, 0) } else { fd, err = os.Open("/proc/kmsg") ck(err) logger.Notice("KLOGD: started with /proc/kmsg as log source\n") } logger.Close() logger, err = syslog.New(syslog.LOG_KERN, "Kernel") ck(err) sigCh := make(chan os.Signal) var sigs []os.Signal for i := syscall.Signal(-1); i != syscall.SIGCHLD; i++ { sigs = append(sigs, i) } signal.Notify(sigCh, sigs...) readCh := make(chan string) go reader(readCh) loop: for { select { case <-sigCh: break loop case str := <-readCh: log(str) } } logger.Notice("KLOGD: Daemon exiting......\n") if *kern { kctl(7, 0) kctl(0, 0) } else { setLogLevel(7) fd.Close() } os.Exit(1) }
func isPidInitialize() bool { ret := false flag.Visit(func(f *flag.Flag) { if "pid_file" == f.Name { ret = true } }) return ret }
func main() { logDir := os.TempDir() flag.Parse() defer glog.Flush() if prof { err := os.MkdirAll("logs", 0777) if err != nil { glog.Fatal(err) } f, err := os.Create("logs/reco-" + strconv.Itoa(nodeID) + ".pprof") if err != nil { fmt.Println("Error: ", err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } // Check if flag log_dir is set and create dir just in case. // (Otherwise glog will ignore it.) flag.Visit(func(f *flag.Flag) { if f.Name == "log_dir" { logDir = f.Value.String() err := os.MkdirAll(logDir, 0777) if err != nil { glog.Fatal(err) } } }) glog.Infof("log_dir: %s", logDir) // donloads movielens data fn := downloadData() // writes train and test data as small data files with ChunkLength lines. dbTrain, dbTest := writeData(fn, nodeID) glog.Infof("train: %s, test: %s", dbTrain, dbTest) config, err := occult.ReadConfig(configFile) if err != nil { glog.Fatal(err) } glog.Infof("Config:\n%s", config) // TODO: Need a way hide this from app. if nodeID > 0 { config.Cluster.NodeID = nodeID } config.App.SetServer(isServer) // Run trainer on multiple nodes. cf := TrainCF(dbTrain, config, ChunkSize) // Run the evaluation on a single node. EvalCF(dbTest, occult.OneNodeConfig(), cf) }
func hasArg(s string) bool { argExist := false flag.Visit(func(f *flag.Flag) { if f.Name == s { argExist = true } }) return argExist }
func hasProvidedConfig() bool { var ret bool flag.Visit(func(f *flag.Flag) { if f.Name == "config" { ret = true } }) return ret }
// flagsAsMap returns a map of all flags that were provided at runtime func flagsAsMap() map[string]interface{} { flags := make(map[string]interface{}) flag.Visit(func(f *flag.Flag) { flags[f.Name] = f.Value.(flag.Getter).Get() }) // Some properties should always be included flags["cpuprofile"] = *cpuprofile flags["memprofile"] = *memprofile return flags }
func main() { log.SetFlags(0) flag.Parse() if *showVersion { fmt.Println("noti version", version) return } if *showHelp { flag.Usage() return } runUtility() if defs := strings.TrimSpace(os.Getenv(defaultEnv)); defs != "" { *banner = strings.Contains(defs, "banner") *speech = strings.Contains(defs, "speech") *pushbullet = strings.Contains(defs, "pushbullet") *slack = strings.Contains(defs, "slack") } else { var explicitSet bool var val bool flag.Visit(func(f *flag.Flag) { if f.Name == "b" || f.Name == "banner" { explicitSet = true // Ignoring error, false on error is fine. val, _ = strconv.ParseBool(f.Value.String()) } }) // If the user explicitly set -banner, then use the value that the user // set, but if no banner flag was set, then the default is true. if explicitSet { *banner = val } else { *banner = true } } if *banner { bannerNotify() } if *speech { speechNotify() } if *pushbullet { pushbulletNotify() } if *slack { slackNotify() } }
func options() map[string]string { options := make(map[string]string) flag.Visit(func(f *flag.Flag) { value := f.Value.String() if f.Name == "password" { value = "<elided>" } options[f.Name] = value }) return options }
func loadActualFlags(flagSet *flag.FlagSet) map[string]string { actual := map[string]string{} fn := func(f *flag.Flag) { actual[f.Name] = f.Name } if nil == flagSet { flag.Visit(fn) } else { flagSet.Visit(fn) } return actual }
// Parse call flag.Parse() and get the command line flags into the appropiate variables. // It is respectful with other packages by not calling flag.Parse() by default to prevent collisions. // If this function is called, it must be INSTEAD of flag.Parse() func (c *GetConf) Parse() { flag.Parse() flag.Visit(func(item *flag.Flag) { for _, v := range c.allOptions { if v.flagName == item.Name { c.Set(v, item.Value.String()) } } }) c.clParsed = true }