本文整理汇总了Golang中flag.FlagSet.Lookup方法的典型用法代码示例。如果您正苦于以下问题:Golang FlagSet.Lookup方法的具体用法?Golang FlagSet.Lookup怎么用?Golang FlagSet.Lookup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flag.FlagSet
的用法示例。
在下文中一共展示了FlagSet.Lookup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: lookupGeneric
func lookupGeneric(name string, set *flag.FlagSet) interface{} {
f := set.Lookup(name)
if f != nil {
return f.Value
}
return nil
}
示例3: 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, " ")
if !visited[name] {
copyFlag(name, ff, set)
}
}
}
return nil
}
示例4: lookupString
func (c *Context) lookupString(name string, set *flag.FlagSet) string {
f := set.Lookup(name)
if f != nil {
return f.Value.String()
} else {
return ""
}
}
示例5: lookupString
func lookupString(name string, set *flag.FlagSet) string {
f := set.Lookup(name)
if f != nil {
return f.Value.String()
}
return ""
}
示例6: getVal
//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()
}
示例7: DebugAddress
func DebugAddress(flags *flag.FlagSet) string {
dbgFlag := flags.Lookup(DebugFlag)
if dbgFlag == nil {
return ""
}
return dbgFlag.Value.String()
}
示例8: lookupIntSlice
func lookupIntSlice(name string, set *flag.FlagSet) []int {
f := set.Lookup(name)
if f != nil {
return (f.Value.(*IntSlice)).Value()
}
return nil
}
示例9: lookupStringSlice
func lookupStringSlice(name string, set *flag.FlagSet) []string {
f := set.Lookup(name)
if f != nil {
return (f.Value.(*StringSlice)).Value()
}
return nil
}
示例10: FlagVisited
// 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
}
示例11: lookupBoolT
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
}
示例12: lookupDuration
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
}
示例13: lookupStringSlice
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
}
示例14: lookupString
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 ""
}
示例15: lookupInt64
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
}