当前位置: 首页>>代码示例>>Golang>>正文


Golang FlagSet.Set方法代码示例

本文整理汇总了Golang中flag.FlagSet.Set方法的典型用法代码示例。如果您正苦于以下问题:Golang FlagSet.Set方法的具体用法?Golang FlagSet.Set怎么用?Golang FlagSet.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在flag.FlagSet的用法示例。


在下文中一共展示了FlagSet.Set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: copyFlag

func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
	switch ff.Value.(type) {
	case *StringSlice:
	default:
		set.Set(name, ff.Value.String())
	}
}
开发者ID:CowLeo,项目名称:gopm,代码行数:7,代码来源:context.go

示例2: SetFlagsFromYaml

// SetFlagsFromYaml goes through all registered flags in the given flagset,
// and if they are not already set it attempts to set their values from
// the YAML config. It will use the key REPLACE(UPPERCASE(flagname), '-', '_')
func SetFlagsFromYaml(fs *flag.FlagSet, rawYaml []byte) (err error) {
	conf := make(map[string]string)
	if err = yaml.Unmarshal(rawYaml, conf); err != nil {
		return
	}
	alreadySet := map[string]struct{}{}
	fs.Visit(func(f *flag.Flag) {
		alreadySet[f.Name] = struct{}{}
	})

	errs := make([]error, 0)
	fs.VisitAll(func(f *flag.Flag) {
		if f.Name == "" {
			return
		}
		if _, ok := alreadySet[f.Name]; ok {
			return
		}
		tag := strings.Replace(strings.ToUpper(f.Name), "-", "_", -1)
		val, ok := conf[tag]
		if !ok {
			return
		}
		if serr := fs.Set(f.Name, val); serr != nil {
			errs = append(errs, fmt.Errorf("invalid value %q for %s: %v", val, tag, serr))
		}
	})
	if len(errs) != 0 {
		err = ErrorSlice(errs)
	}
	return
}
开发者ID:Celluliodio,项目名称:flannel,代码行数:35,代码来源:yaml.go

示例3: SetFlagsFromEnv

// SetFlagsFromEnv parses all registered flags in the given flagset,
// and if they are not already set it attempts to set their values from
// environment variables. Environment variables take the name of the flag but
// are UPPERCASE, have the prefix "PREFIX_", and any dashes are replaced by
// underscores - for example: some-flag => PREFIX_SOME_FLAG
func SetFlagsFromEnv(fs *flag.FlagSet, prefix string) error {
	var err error
	alreadySet := make(map[string]bool)
	fs.Visit(func(f *flag.Flag) {
		alreadySet[f.Name] = true
	})

	if prefix == "" {
		prefix = "_"
	} else {
		prefix = strings.ToUpper(strings.Replace(prefix, "-", "_", -1))
	}

	fs.VisitAll(func(f *flag.Flag) {
		if !alreadySet[f.Name] {
			key := prefix + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
			val := os.Getenv(key)
			if val != "" {
				if serr := fs.Set(f.Name, val); serr != nil {
					err = fmt.Errorf("invalid value %q for %s: %v", val, key, serr)
				}
			}
		}
	})
	return err
}
开发者ID:gooops,项目名称:staffio,代码行数:31,代码来源:config.go

示例4: normalizeFlags

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, " ")
			set.Set(name, ff.Value.String())
		}
	}
	return nil
}
开发者ID:jaredly,项目名称:cli,代码行数:30,代码来源:context.go

示例5: ParseConfig

// 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
}
开发者ID:nelhage,项目名称:go.cli,代码行数:28,代码来源:config.go

示例6: copyFlag

func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
	switch ff.Value.(type) {
	case Serializeder:
		set.Set(name, ff.Value.(Serializeder).Serialized())
	default:
		set.Set(name, ff.Value.String())
	}
}
开发者ID:getcfs,项目名称:cfs-binary-release,代码行数:8,代码来源:context.go

示例7: SetBindAddrFromAddr

// SetBindAddrFromAddr sets the value of bindAddr flag from the value
// of addr flag. Both flags' Value must be of type IPAddressPort. If the
// bindAddr flag is set and the addr flag is unset, it will set bindAddr to
// [::]:port of addr. Otherwise, it keeps the original values.
func SetBindAddrFromAddr(fs *flag.FlagSet, bindAddrFlagName, addrFlagName string) {
	if IsSet(fs, bindAddrFlagName) || !IsSet(fs, addrFlagName) {
		return
	}
	addr := *fs.Lookup(addrFlagName).Value.(*IPAddressPort)
	addr.IP = "::"
	if err := fs.Set(bindAddrFlagName, addr.String()); err != nil {
		plog.Panicf("unexpected flags set error: %v", err)
	}
}
开发者ID:johnchengliu,项目名称:etcd,代码行数:14,代码来源:flag.go

示例8: ParseSet

// ParseSet parses the config file for the provided flag set.
// If the flags are already set, values are overwritten
// by the values in the config file. Defaults are not set
// if the flag is not in the file.
func (g *GlobalConf) ParseSet(flagSetName string, set *flag.FlagSet) {
	set.VisitAll(func(f *flag.Flag) {
		if val, ok := getEnv(g.EnvPrefix, flagSetName, f.Name); ok {
			set.Set(f.Name, val)
			return
		}

		val, found := g.dict.GetString(flagSetName, f.Name)
		if found {
			set.Set(f.Name, val)
		}
	})
}
开发者ID:rakyll,项目名称:globalconf,代码行数:17,代码来源:globalconf.go

示例9: ParseSet

// Parses the config file for the provided flag set.
// If the flags are already set, values are overwritten
// by the values in the config file. Defaults are not set
// if the flag is not in the file.
func (g *GlobalConf) ParseSet(flagSetName string, set *flag.FlagSet) {
	set.VisitAll(func(f *flag.Flag) {
		val := getEnv(flagSetName, f.Name)
		if val != "" {
			set.Set(f.Name, val)
			return
		}

		val, found := g.dict.GetString(flagSetName, f.Name)
		if found {
			set.Set(f.Name, val)
		}
	})
}
开发者ID:shaunstanislaus,项目名称:fleet,代码行数:18,代码来源:globalconf.go

示例10: flagsFromEnv

// flagsFromEnv parses all registered flags in the given flagset,
// and if they are not already set it attempts to set their values from
// environment variables. Environment variables take the name of the flag but
// are UPPERCASE, have the given prefix, and any dashes are replaced by
// underscores - for example: some-flag => PREFIX_SOME_FLAG
func flagsFromEnv(prefix string, fs *flag.FlagSet) {
	alreadySet := make(map[string]bool)
	fs.Visit(func(f *flag.Flag) {
		alreadySet[f.Name] = true
	})
	fs.VisitAll(func(f *flag.Flag) {
		if !alreadySet[f.Name] {
			key := strings.ToUpper(prefix + "_" + strings.Replace(f.Name, "-", "_", -1))
			val := os.Getenv(key)
			if val != "" {
				fs.Set(f.Name, val)
			}
		}
	})
}
开发者ID:eldarion-gondor,项目名称:cli,代码行数:20,代码来源:main.go

示例11: ParseFlags

// 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
}
开发者ID:tomzhang,项目名称:p2,代码行数:16,代码来源:param.go

示例12: AddEnvironmentToFlags

// from github.com/coreos/etcd
//
// SetFlagsFromEnv parses all registered flags in the given flagset,
// and if they are not already set it attempts to set their values from
// environment variables. Environment variables take the name of the flag but
// are UPPERCASE, have the prefix `s`, and any dashes are replaced by
// underscores - for example: some-flag => ETCD_SOME_FLAG
func AddEnvironmentToFlags(s string, fs *flag.FlagSet) error {
	var err error
	alreadySet := make(map[string]bool)
	fs.Visit(func(f *flag.Flag) {
		alreadySet[f.Name] = true
	})
	fs.VisitAll(func(f *flag.Flag) {
		if !alreadySet[f.Name] {
			key := s + "_" + ToEnvironmentKey(f.Name)
			val := os.Getenv(key)
			if val != "" {
				if serr := fs.Set(f.Name, val); serr != nil {
					err = fmt.Errorf("invalid value %q for %s: %v", val, key, serr)
				}
			}
		}
	})
	return err
}
开发者ID:ibotty,项目名称:leader-elect,代码行数:26,代码来源:master-elect.go

示例13: SetFlagsFromEnv

// SetFlagsFromEnv parses all registered flags in the given flagset,
// and if they are not already set it attempts to set their values from
// environment variables. Environment variables take the name of the flag but
// are UPPERCASE, have the prefix "ETCD_", and any dashes are replaced by
// underscores - for example: some-flag => ETCD_SOME_FLAG
func SetFlagsFromEnv(fs *flag.FlagSet) error {
	var err error
	alreadySet := make(map[string]bool)
	fs.Visit(func(f *flag.Flag) {
		alreadySet[flagToEnv(f.Name)] = true
	})
	usedEnvKey := make(map[string]bool)
	fs.VisitAll(func(f *flag.Flag) {
		key := flagToEnv(f.Name)
		if !alreadySet[key] {
			val := os.Getenv(key)
			if val != "" {
				usedEnvKey[key] = true
				if serr := fs.Set(f.Name, val); serr != nil {
					err = fmt.Errorf("invalid value %q for %s: %v", val, key, serr)
				}
				plog.Infof("recognized and used environment variable %s=%s", key, val)
			}
		}
	})

	for _, env := range os.Environ() {
		kv := strings.SplitN(env, "=", 2)
		if len(kv) != 2 {
			plog.Warningf("found invalid env %s", env)
		}
		if usedEnvKey[kv[0]] {
			continue
		}
		if alreadySet[kv[0]] {
			plog.Infof("recognized environment variable %s, but unused: shadowed by corresponding flag ", kv[0])
			continue
		}
		if strings.HasPrefix(env, "ETCD_") {
			plog.Warningf("unrecognized environment variable %s", env)
		}
	}

	return err
}
开发者ID:carriercomm,项目名称:etcd,代码行数:45,代码来源:flag.go

示例14: Populate

// Populate sets the flags in set from the environment. The
// environment value used will be the name of the flag in upper case,
// with '-' changed to '_', and (if prefixis not the empty string)
// prepended with prefix and an underscore. So, if prefix is
// "DOORMAN", and the flag's name "foo-bar", the environment variable
// DOORMAN_FOO_BAR will be used.
func Populate(set *flag.FlagSet, prefix string) error {
	var (
		setThroughFlags = make(map[string]bool)
		knownEnv        = make(map[string]bool)
		err             error
	)
	set.Visit(func(f *flag.Flag) {
		setThroughFlags[f.Name] = true
	})

	set.VisitAll(func(f *flag.Flag) {
		key := flagToEnv(prefix, f.Name)
		knownEnv[key] = true
		val := os.Getenv(key)
		if val == "" {
			return
		}

		if setThroughFlags[f.Name] {
			log.Warningf("Recognized environment variable %v, but shadowed by flag %v: won't be used.", key, f.Name)
			return
		}
		if e := set.Set(f.Name, val); e != nil {
			err = fmt.Errorf("Invalid value %q for %v.", val, key)
			return
		}
	})

	for _, env := range os.Environ() {
		kv := strings.SplitN(env, "=", 2)
		if len(kv) < 1 {
			continue
		}
		if name := kv[0]; strings.HasPrefix(name, prefix) && !knownEnv[name] {
			log.Warningf("Unrecognized environment variable %s", name)
		}
	}

	return err
}
开发者ID:youtube,项目名称:doorman,代码行数:46,代码来源:flagenv.go

示例15: SetFlagsFromEnvFile

// SetFlagsFromEnvFile iterates the given flagset and if any flags are not
// already set it attempts to set their values from the given env file. Env
// files may have KEY=VALUE lines where the environment variable names are
// in UPPERCASE, prefixed by the given PREFIX, and dashes are replaced by
// underscores. For example, if prefix=PREFIX, some-flag is named
// PREFIX_SOME_FLAG.
// Comment lines are skipped, but more complex env file parsing is not
// performed.
func SetFlagsFromEnvFile(fs *flag.FlagSet, prefix string, path string) (err error) {
	alreadySet := make(map[string]bool)
	fs.Visit(func(f *flag.Flag) {
		alreadySet[f.Name] = true
	})
	envs, err := parseEnvFile(path)
	if err != nil {
		return err
	}
	fs.VisitAll(func(f *flag.Flag) {
		if !alreadySet[f.Name] {
			key := prefix + "_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
			val := envs[key]
			if val != "" {
				if serr := fs.Set(f.Name, val); serr != nil {
					err = fmt.Errorf("invalid value %q for %s: %v", val, key, serr)
				}
			}
		}
	})
	return err
}
开发者ID:Celluliodio,项目名称:flannel,代码行数:30,代码来源:env_file.go


注:本文中的flag.FlagSet.Set方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。