// ProcessFlagWithEnv parses flag from env.
// This function also add ENVIRONMENT VARIABLE to usage.
// NOTICE: flag.Parse() will not be called by this function.
480258
func (ef EnvFlag) ProcessFlagWithEnv() error {
	if ef.flagSet.Parsed() {
		return errors.New("flag has already been parsed")
	}
	flagEnvMap := map[string]string{}
	for k, v := range ef.envFlagDict {
		flagEnvMap[v] = k
	}
	// Rewrite flag.Useage and show env key in usage
	if ef.showEnvKeyInUsage {
		ef.flagSet.VisitAll(func(f *flag.Flag) {
			if len(f.Name) < ef.minLength {
				return
			}
			envKey, ok := flagEnvMap[f.Name]
			if !ok {
				envKey = flagToEnv(f.Name)
			}
			envPrefix := fmt.Sprintf("[%s]", envKey)
			if strings.HasPrefix(f.Usage, envPrefix) {
				return
			}
			f.Usage = fmt.Sprintf("%s %s", envPrefix, f.Usage)
		})
	}

	for _, envLine := range os.Environ() {
		debug("Find a new line of environment variable, ", envLine)
		envKV := strings.SplitN(envLine, "=", 2)
		var key, value string
		key = envKV[0]
		if len(key) < ef.minLength {
			continue
		}
		if len(envKV) > 1 {
			value = envKV[1]
		} else {
			value = ""
		}

		var flagKey string
		if userFlag, ok := ef.envFlagDict[key]; ok {
			flagKey = userFlag
		} else {
			flagKey = envToFlag(key)
		}
		debug("ENV ", key, " is converted to ", flagKey)
		f := ef.flagSet.Lookup(flagKey)
		if f == nil {
			debug(flagKey, " is not defined in flag, skip!")
			continue
		}

		if ef.showEnvValInUsage {
			// use env value as default value (in usage)
			f.DefValue = value
		}

		debug("Set  [", flagKey, ",", value, "] to flag")
		if err := ef.flagSet.Set(flagKey, value); err != nil {
			return fmt.Errorf("error when set [%s,%s] into flag\n", flagKey, value)
		}
	}
	return nil
}
120063
func (tn *tagNode) ProcessWith(p Processor) error {
	if err := p.OpenTag(tn.tag); err != nil {
		return err
	}
	for _, child := range tn.children {
		if err := child.ProcessWith(p); err != nil {
			return err
		}
	}
	return p.CloseTag(tn.tag)
}
1246669
func (f DurationFlag) ApplyWithError(set *flag.FlagSet) error {
	if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
		envValDuration, err := time.ParseDuration(envVal)
		if err != nil {
			return fmt.Errorf("could not parse %s as duration for flag %s: %s", envVal, f.Name, err)
		}

		f.Value = envValDuration
	}

	eachName(f.Name, func(name string) {
		if f.Destination != nil {
			set.DurationVar(f.Destination, name, f.Value, f.Usage)
			return
		}
		set.Duration(name, f.Value, f.Usage)
	})

	return nil
}
412951
func Env(key, fallback string) string {
	v := os.Getenv(key)
	if v != "" {
		return v
	}

	return fallback
}
1074628
func (l *Logger) WithFlags(flags int, f func(...interface{}), args ...interface{}) {
	cFlags := l.flags
	l.SetFlags(flags)
	f(args...)
	l.SetFlags(cFlags)
}
273414
func (s *Lex) ProcessWordWithMap(word string, env map[string]string) (string, error) {
	word, _, err := s.process(word, env)
	return word, err
}
1035350
func WithFlags(flags ...string) func(*icmd.Cmd) func() {
	return func(cmd *icmd.Cmd) func() {
		cmd.Command = append(cmd.Command, flags...)
		return nil
	}
}
258571
func (f UintFlag) ApplyWithError(set *flag.FlagSet) error {
	if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
		envValInt, err := strconv.ParseUint(envVal, 0, 64)
		if err != nil {
			return fmt.Errorf("could not parse %s as uint value for flag %s: %s", envVal, f.Name, err)
		}

		f.Value = uint(envValInt)
	}

	eachName(f.Name, func(name string) {
		if f.Destination != nil {
			set.UintVar(f.Destination, name, f.Value, f.Usage)
			return
		}
		set.Uint(name, f.Value, f.Usage)
	})

	return nil
}
809211
func setFlagFromEnv(name, envar string, fs *pflag.FlagSet) {
	if fs.Changed(name) {
		return
	}
	if v, ok := os.LookupEnv(envar); ok {
		fs.Set(name, v)
	}
}
1237520
func FlagToEnv(s string) string {
	return strings.ToUpper(strings.Join(strings.Split(s, flagDivider), envDivider))
}
654191
func Flag(name string, defv interface{}, usage string) V {
	return flagA(flagT{
		name:  name,
		defv:  defv,
		usage: usage,
	})
}
1303861
func parseFlags(s reflect.Value) error {
	flagSet := flag.NewFlagSet("conf", flag.ExitOnError)
	flagSet.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flagSet.PrintDefaults()
	}
	typeOfT := s.Type()
	for i := 0; i < s.NumField(); i++ {
		fieldValue := s.Field(i)
		if fieldValue.CanSet() {
			createFlag(typeOfT.Field(i), fieldValue, flagSet)
		}
	}
	args := os.Args[1:]
	if hasTestFlag(os.Args[1:]) {
		args = []string{}
	}
	err := flagSet.Parse(args)
	if err != nil {
		return err
	}
	return nil
}
1171376
func (f Int64Flag) ApplyWithError(set *flag.FlagSet) error {
	if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
		envValInt, err := strconv.ParseInt(envVal, 0, 64)
		if err != nil {
			return fmt.Errorf("could not parse %s as int value for flag %s: %s", envVal, f.Name, err)
		}

		f.Value = envValInt
	}

	eachName(f.Name, func(name string) {
		if f.Destination != nil {
			set.Int64Var(f.Destination, name, f.Value, f.Usage)
			return
		}
		set.Int64(name, f.Value, f.Usage)
	})

	return nil
}
1296154
func (cl *CommandLine) parseSingleFlag(fs *parsedFlagState) error {
	// Consume the first character from flag into "r". "flag" is the remainder.
	r, l := utf8.DecodeRuneInString(fs.flag)
	if r == utf8.RuneError {
		return errors.Reason("invalid rune in flag").Err()
	}
	fs.flag = fs.flag[l:]

	// Retrieve the value for a non-binary flag. This mutates the flag state to
	// consume that value.
	getFlagValue := func() (val string, err error) {
		switch {
		case len(fs.flag) > 0:
			// Combined flag/value (e.g., -c'paoskdpo')
			val, fs.flag = fs.flag, ""
		case len(fs.args) == 0:
			err = errors.New("two-value flag missing second value")
		default:
			// Flag value is in subsequent argument (e.g., "-c 'paoskdpo'").
			// Consume the argument.
			val, fs.args = fs.args[0], fs.args[1:]
		}
		return
	}

	// Some cases will set this to true if `r` is determined to just be a no-value
	// single-character flag
	isSingleCharFlag := false

	switch r {
	case 'c':
		// Inline command target.
		val, err := getFlagValue()
		if err != nil {
			return err
		}
		cl.Target = CommandTarget{val}

	case 'm':
		// Python module target.
		val, err := getFlagValue()
		if err != nil {
			return err
		}
		cl.Target = ModuleTarget{val}

	case 'Q', 'W', 'X':
		// Two-argument Python flags.
		val, err := getFlagValue()
		if err != nil {
			return err
		}
		cl.Flags = append(cl.Flags, CommandLineFlag{string(r), val})

	case 'O':
		// Handle the case of the odd flag "-OO", which parses as a single flag.
		var has bool
		if fs.flag, has = trimPrefix(fs.flag, "O"); has {
			cl.Flags = append(cl.Flags, CommandLineFlag{"OO", ""})
			break
		}

		// Single "O", do normal single-flag parsing.
		isSingleCharFlag = true

	case '-':
		// handle the case of "--version", which is an atypical many-character flag.
		if fs.flag == "version" {
			fs.flag = ""
			cl.Flags = append(cl.Flags, CommandLineFlag{"-version", ""})
			break
		}

		// Not sure what this could be, but fall through none the less.
		isSingleCharFlag = true

	default:
		isSingleCharFlag = true
	}

	if isSingleCharFlag {
		// One-argument Python flags. If there are more characters in "flag",
		// don't consume the entire flag; instead, replace it with the remainder
		// for subsequent parses. This handles cases like "-vvc <script>".
		cl.Flags = append(cl.Flags, CommandLineFlag{string(r), ""})
	}

	return nil
}
50139
func InitFlags() {
	pflag.CommandLine.SetNormalizeFunc(WordSepNormalizeFunc)
	pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
	pflag.Parse()
	pflag.VisitAll(func(flag *pflag.Flag) {
		klog.V(2).Infof("FLAG: --%s=%q", flag.Name, flag.Value)
	})
}
248961
func HandleFlags(f *Flags) bool {
	if !f.HasMatch() {
		return false
	}

	if f.ShowHelp() {
		f.PrintHelp()
	}

	if f.ShowVersion() {
		f.PrintVersion()
	}

	return true
}
30528
func AddEnv(env_str string) error {
	result := regexp.MustCompile("([^:]+):([^=]+)=(.+)").FindStringSubmatch(env_str)
	if result==nil {
		return fmt.Errorf("Error parsing internal service environment variable: %s", env_str)
	}
	service := result[1]
	key     := result[2]
	val     := result[3]

	if serviceMap, ok := envPerService[service]; ok {
		serviceMap[key] = val
		return nil
	}
	svcNames := []string{}
	for key := range envPerService {
		svcNames = append(svcNames, key)
	}
	return fmt.Errorf("Error setting internal service environment:'%s'  Service '%s' must be one of [%s]'",
		env_str, service, strings.Join(svcNames, ", "))
}