Example #1
0
// ParseConfig parses a config file, using the provided FlagSet to
// look up, parse, and store values.
func ParseConfig(flags *flag.FlagSet, f io.Reader) error {
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		if line == "" || strings.HasPrefix(line, "#") {
			continue
		}

		bits := strings.SplitN(line, "=", 2)
		if len(bits) != 2 {
			return fmt.Errorf("illegal config line: `%s'", line)
		}

		key := strings.TrimSpace(bits[0])
		value := strings.TrimSpace(bits[1])

		if flag := flags.Lookup(key); flag == nil {
			return fmt.Errorf("unknown option `%s'", key)
		}

		if err := flags.Set(key, value); err != nil {
			return err
		}
	}
	return nil
}
Example #2
0
func lookupGeneric(name string, set *flag.FlagSet) interface{} {
	f := set.Lookup(name)
	if f != nil {
		return f.Value
	}
	return nil
}
Example #3
0
func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
	visited := make(map[string]bool)
	set.Visit(func(f *flag.Flag) {
		visited[f.Name] = true
	})
	for _, f := range flags {
		parts := strings.Split(f.getName(), ",")
		if len(parts) == 1 {
			continue
		}
		var ff *flag.Flag
		for _, name := range parts {
			name = strings.Trim(name, " ")
			if visited[name] {
				if ff != nil {
					return errors.New("Cannot use two forms of the same flag: " + name + " " + ff.Name)
				}
				ff = set.Lookup(name)
			}
		}
		if ff == nil {
			continue
		}
		for _, name := range parts {
			name = strings.Trim(name, " ")
			if !visited[name] {
				copyFlag(name, ff, set)
			}
		}
	}
	return nil
}
Example #4
0
func (c *Context) lookupString(name string, set *flag.FlagSet) string {
	f := set.Lookup(name)
	if f != nil {
		return f.Value.String()
	} else {
		return ""
	}
}
Example #5
0
func lookupString(name string, set *flag.FlagSet) string {
	f := set.Lookup(name)
	if f != nil {
		return f.Value.String()
	}

	return ""
}
Example #6
0
//Retrieves the argument from the provided flagset with the give name
func getVal(set *flag.FlagSet, arg string) interface{} {
	requestedFlag := set.Lookup(arg)
	if requestedFlag == nil {
		logrus.Debugf("Could not find requested flag: %s", arg)
		return nil
	}
	return requestedFlag.Value.(flag.Getter).Get()
}
Example #7
0
func DebugAddress(flags *flag.FlagSet) string {
	dbgFlag := flags.Lookup(DebugFlag)
	if dbgFlag == nil {
		return ""
	}

	return dbgFlag.Value.String()
}
Example #8
0
func lookupIntSlice(name string, set *flag.FlagSet) []int {
	f := set.Lookup(name)
	if f != nil {
		return (f.Value.(*IntSlice)).Value()

	}

	return nil
}
Example #9
0
func lookupStringSlice(name string, set *flag.FlagSet) []string {
	f := set.Lookup(name)
	if f != nil {
		return (f.Value.(*StringSlice)).Value()

	}

	return nil
}
Example #10
0
File: utils.go Project: hawx/img
// FlagVisited determines whether the named flag has been visited in the FlagSet
// given. This is helpful if you want to have a flag that triggers an action
// when given, but is not a boolean flag.
func FlagVisited(name string, flags flag.FlagSet) (didFind bool) {
	toFind := flags.Lookup(name)

	flags.Visit(func(f *flag.Flag) {
		if f == toFind {
			didFind = true
		}
	})

	return
}
Example #11
0
func lookupBoolT(name string, set *flag.FlagSet) bool {
	f := set.Lookup(name)
	if f != nil {
		parsed, err := strconv.ParseBool(f.Value.String())
		if err != nil {
			return false
		}
		return parsed
	}
	return false
}
Example #12
0
func lookupDuration(name string, set *flag.FlagSet) time.Duration {
	f := set.Lookup(name)
	if f != nil {
		val, err := time.ParseDuration(f.Value.String())
		if err == nil {
			return val
		}
	}

	return 0
}
Example #13
0
func lookupStringSlice(name string, set *flag.FlagSet) []string {
	f := set.Lookup(name)
	if f != nil {
		parsed, err := (f.Value.(*StringSlice)).Value(), error(nil)
		if err != nil {
			return nil
		}
		return parsed
	}
	return nil
}
Example #14
0
func lookupString(name string, set *flag.FlagSet) string {
	f := set.Lookup(name)
	if f != nil {
		parsed, err := f.Value.String(), error(nil)
		if err != nil {
			return ""
		}
		return parsed
	}
	return ""
}
Example #15
0
func lookupInt64(name string, set *flag.FlagSet) int64 {
	f := set.Lookup(name)
	if f != nil {
		parsed, err := strconv.ParseInt(f.Value.String(), 0, 64)
		if err != nil {
			return 0
		}
		return parsed
	}
	return 0
}
Example #16
0
func lookupGeneric(name string, set *flag.FlagSet) interface{} {
	f := set.Lookup(name)
	if f != nil {
		parsed, err := f.Value, error(nil)
		if err != nil {
			return nil
		}
		return parsed
	}
	return nil
}
Example #17
0
func lookupUint(name string, set *flag.FlagSet) uint {
	f := set.Lookup(name)
	if f != nil {
		parsed, err := strconv.ParseUint(f.Value.String(), 0, 64)
		if err != nil {
			return 0
		}
		return uint(parsed)
	}
	return 0
}
Example #18
0
// UpdateLoggingFlagsFromConfig extracts the logging-related options from
// the provided config and sets flags in the given flagset
func UpdateLoggingFlagsFromConfig(flagset *flag.FlagSet, conf *Config) {
	err := flagset.Lookup("v").Value.Set(strconv.Itoa(conf.Verbosity))
	if err != nil {
		glog.Errorf("Failed to apply config.Verbosity to flag.v: %v", err)
	}

	err = flagset.Lookup("logtostderr").Value.Set("true")
	if err != nil {
		glog.Errorf("Failed to set flag.logtostderr to true: %v", err)
	}
}
Example #19
0
func SplitFlagsetFromArgs(flagset *flag.FlagSet, args []string) (flagsetArgs []string, additionalArgs []string) {
	for _, f := range args {
		if flagset.Lookup(getFlagName(f)) != nil {
			flagsetArgs = append(flagsetArgs, f)
		} else {
			additionalArgs = append(additionalArgs, f)
		}
	}

	return
}
Example #20
0
func lookupInt(name string, set *flag.FlagSet) int {
	f := set.Lookup(name)
	if f != nil {
		val, err := strconv.Atoi(f.Value.String())
		if err != nil {
			return 0
		}
		return val
	}

	return 0
}
Example #21
0
func lookupFloat64(name string, set *flag.FlagSet) float64 {
	f := set.Lookup(name)
	if f != nil {
		val, err := strconv.ParseFloat(f.Value.String(), 64)
		if err != nil {
			return 0
		}
		return val
	}

	return 0
}
Example #22
0
func lookupInt(name string, set *flag.FlagSet) int {
	f := set.Lookup(name)
	if f != nil {
		val, err := strconv.ParseInt(f.Value.String(), 0, 64)
		if err != nil {
			return 0
		}
		return int(val)
	}

	return 0
}
Example #23
0
File: context.go Project: Jacke/cli
func (c *Context) lookupBool(name string, set *flag.FlagSet) bool {
	f := set.Lookup(name)
	if f != nil {
		val, err := strconv.ParseBool(f.Value.String())
		if err != nil {
			return false
		}
		return val
	}

	return false
}
Example #24
0
func completeFlags(cl CommandLine, flags *flag.FlagSet) (completions []string, rest CommandLine) {
	if len(cl) == 0 {
		return nil, cl
	}
	var inFlag string
	for len(cl) > 1 {
		w := cl[0]
		if inFlag != "" {
			inFlag = ""
		} else if len(w) > 1 && w[0] == '-' && w != "--" {
			if !strings.Contains(w, "=") {
				var i int
				for i = 0; i < len(w) && w[i] == '-'; i++ {
				}
				inFlag = w[i:]
			}
			if flag := flags.Lookup(inFlag); flag != nil {
				if bf, ok := flag.Value.(boolFlag); ok && bf.IsBoolFlag() {
					inFlag = ""
				}
			}
		} else {
			if w == "--" {
				cl = cl[1:]
			}
			return nil, cl
		}
		cl = cl[1:]
	}

	if inFlag != "" {
		// Complete a flag value. No-op for now.
		return []string{}, nil
	} else if len(cl[0]) > 0 && cl[0][0] == '-' {
		// complete a flag name
		prefix := strings.TrimLeft(cl[0], "-")
		flags.VisitAll(func(f *flag.Flag) {
			if strings.HasPrefix(f.Name, prefix) {
				completions = append(completions, "-"+f.Name)
			}
		})
		return completions, nil
	}

	if cl[0] == "" {
		flags.VisitAll(func(f *flag.Flag) {
			completions = append(completions, "-"+f.Name)
		})
	}
	return completions, cl
}
Example #25
0
File: param.go Project: tomzhang/p2
// ParseFlags parses a map of parameter assignments that are defined by a FlagSet.
func ParseFlags(f *flag.FlagSet, values Values) error {
	for name := range values {
		if f.Lookup(name) == nil {
			return fmt.Errorf("%s: no such parameter", name)
		}
	}
	var err error
	for name, value := range values {
		err = f.Set(name, value)
		if err != nil {
			err = fmt.Errorf("%s: %s", name, err)
		}
	}
	return err
}
Example #26
0
func assignFlagSet(prefix string, res map[string]interface{}, flagSet *flag.FlagSet, actual map[string]string, isOverride bool) error {
	for k, v := range res {
		switch value := v.(type) {
		case map[string]interface{}:
			e := assignFlagSet(combineName(prefix, k), value, flagSet, actual, isOverride)
			if nil != e {
				return e
			}
			continue
		case []interface{}:
		case string:
		case float64:
		case bool:
		case nil:
			continue
		default:
			return fmt.Errorf("unsupported type for %s - %T", combineName(prefix, k), v)
		}
		nm := combineName(prefix, k)

		if !isOverride {
			if _, ok := actual[nm]; ok {
				log.Printf("load flag '%s' from config is skipped.\n", nm)
				continue
			}
		}

		var g *flag.Flag
		if nil == flagSet {
			g = flag.Lookup(nm)
		} else {
			g = flagSet.Lookup(nm)
		}

		if nil == g {
			log.Printf("flag '%s' is not defined.\n", nm)
			continue
		}

		err := g.Value.Set(fmt.Sprint(v))
		if nil != err {
			return err
		}
	}
	return nil
}
Example #27
0
// UpdateLoggingFlagsFromConfig extracts the logging-related options from
// the provided config and sets flags in the given flagset
func UpdateLoggingFlagsFromConfig(flagset *flag.FlagSet, conf *Config) {
	err := flagset.Lookup("v").Value.Set(strconv.Itoa(conf.Verbosity))
	if err != nil {
		glog.Errorf("Failed to apply config.Verbosity to flag.v: %v", err)
	}

	err = flagset.Lookup("logtostderr").Value.Set("true")
	if err != nil {
		glog.Errorf("Failed to set flag.logtostderr to true: %v", err)
	}

	if conf.Verbosity > 2 {
		etcd.SetLogger(log.New(os.Stdout, "go-etcd", log.LstdFlags))
	} else {
		etcd.SetLogger(log.New(ioutil.Discard, "go-etcd", log.LstdFlags))
	}
}
Example #28
0
func mustHave(fset *flag.FlagSet, keys ...string) error {
	for _, key := range keys {
		found := false
		fset.Visit(
			func(f *flag.Flag) {
				if f.Name == key {
					found = true
				}
			})
		if !found {
			flag := fset.Lookup(key)
			if flag == nil || flag.DefValue == "" {
				return fmt.Errorf("Invalid flags. Flag '%s' is required for this operation", key)
			}
		}
	}
	return nil
}
Example #29
0
func setupInternalFlags(fs *flag.FlagSet) {
	if fs.Lookup("c") == nil {
		fs.StringVar(&internalConfig.ConfigFile, "c", "", "Specify flagconf configuration")
	} else if fs.Lookup("C") == nil {
		fs.StringVar(&internalConfig.ConfigFile, "C", "", "Specify flagconf configuration")
	} else if fs.Lookup("flagconfFile") == nil {
		fs.StringVar(&internalConfig.ConfigFile, "flagconfFile", "", "Specify flagconf configuration")
	}

	if fs.Lookup("flagconfConfirm") == nil {
		fs.BoolVar(&internalConfig.RequireConfirmation, "flagconfConfirm", false, "Require confirmation from user ro use flag configuration")
	}
}
Example #30
0
func peerTLSInfo(fs *flag.FlagSet) *TLSInfo {
	return &TLSInfo{
		CAFile:   fs.Lookup("peer-ca-file").Value.String(),
		CertFile: fs.Lookup("peer-cert-file").Value.String(),
		KeyFile:  fs.Lookup("peer-key-file").Value.String(),
	}
}