• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang flag.Value类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: setFromEnv

func setFromEnv(into flag.Value, envVars string) bool {
	multiValued, isMulti := into.(multiValued)

	if len(envVars) > 0 {
		for _, rev := range strings.Split(envVars, " ") {
			ev := strings.TrimSpace(rev)
			if len(ev) == 0 {
				continue
			}

			v := os.Getenv(ev)
			if len(v) == 0 {
				continue
			}
			if !isMulti {
				if err := into.Set(v); err == nil {
					return true
				}
				continue
			}

			vs := strings.Split(v, ",")
			if err := setMultivalued(multiValued, vs); err == nil {
				return true
			}
		}
	}
	return false
}
开发者ID:jault3,项目名称:mow.cli,代码行数:29,代码来源:utils.go


示例2: Var

// Var defines a flag with the specified name and usage string. The type and
// value of the flag are represented by the first argument, of type Value, which
// typically holds a user-defined implementation of Value. For instance, the
// caller could create a flag that turns a comma-separated string into a slice
// of strings by giving the slice the methods of Value; in particular, Set would
// decompose the comma-separated string into the slice.
func (c *ConfigoSet) Var(value flag.Value, name string, usage string, isFlag, isConfig bool) {
	// Remember the default value as a string; it won't change.
	config := &Configo{name, usage, value, value.String(), isFlag, isConfig}
	_, alreadythere := c.formal[name]
	if alreadythere {
		msg := fmt.Sprintf("%s flag redefined: %s", c.name, name)
		fmt.Fprintln(c.out(), msg)
		panic(msg) // Happens only if flags are declared with identical names
	}
	if c.formal == nil {
		c.formal = make(map[string]*Configo)
	}
	c.formal[name] = config
}
开发者ID:quincy,项目名称:configo,代码行数:20,代码来源:configo.go


示例3: Var

// Var sets the value, name, and usage for an environment variable in the set
func (e *Set) Var(value flag.Value, name string, usage string) {
	v := &Variable{name, usage, value, value.String()}
	_, alreadythere := e.variables[name]
	if alreadythere {
		var msg string
		if e.name == "" {
			msg = fmt.Sprintf("flag redefined: %s", name)
		} else {
			msg = fmt.Sprintf("%s flag redefined: %s", e.name, name)
		}
		fmt.Fprintln(e.out(), msg)
		panic(msg) // Happens only if flags are declared with identical names
	}
	if e.variables == nil {
		e.variables = make(map[string]*Variable)
	}
	e.variables[name] = v
}
开发者ID:doubledutch,项目名称:go-env,代码行数:19,代码来源:env.go


示例4: TestIPNet

func TestIPNet(t *testing.T) {
	testCases := []struct {
		input    string
		success  bool
		expected string
	}{
		{"0.0.0.0/0", true, "0.0.0.0/0"},
		{" 0.0.0.0/0 ", true, "0.0.0.0/0"},
		{"1.2.3.4/8", true, "1.0.0.0/8"},
		{"127.0.0.1/16", true, "127.0.0.0/16"},
		{"255.255.255.255/19", true, "255.255.224.0/19"},
		{"255.255.255.255/32", true, "255.255.255.255/32"},
		{"", false, ""},
		{"/0", false, ""},
		{"0", false, ""},
		{"0/0", false, ""},
		{"localhost/0", false, ""},
		{"0.0.0/4", false, ""},
		{"0.0.0./8", false, ""},
		{"0.0.0.0./12", false, ""},
		{"0.0.0.256/16", false, ""},
		{"0.0.0.0 /20", false, ""},
		{"0.0.0.0/ 24", false, ""},
		{"0 . 0 . 0 . 0 / 28", false, ""},
		{"0.0.0.0/33", false, ""},
	}

	for i := range testCases {
		tc := &testCases[i]
		var f flag.Value = &IPNet{}
		err := f.Set(tc.input)
		if err != nil && tc.success == true {
			t.Errorf("expected success, got %q", err)
			continue
		} else if err == nil && tc.success == false {
			t.Errorf("expected failure")
			continue
		} else if tc.success {
			if f.String() != tc.expected {
				t.Errorf("expected %q, got %q", tc.expected, f.String())
			}
		}
	}
}
开发者ID:ericcapricorn,项目名称:kubernetes,代码行数:44,代码来源:net_test.go


示例5: TestIP

func TestIP(t *testing.T) {
	testCases := []struct {
		input    string
		success  bool
		expected string
	}{
		{"0.0.0.0", true, "0.0.0.0"},
		{" 0.0.0.0 ", true, "0.0.0.0"},
		{"1.2.3.4", true, "1.2.3.4"},
		{"127.0.0.1", true, "127.0.0.1"},
		{"255.255.255.255", true, "255.255.255.255"},
		{"", false, ""},
		{"0", false, ""},
		{"localhost", false, ""},
		{"0.0.0", false, ""},
		{"0.0.0.", false, ""},
		{"0.0.0.0.", false, ""},
		{"0.0.0.256", false, ""},
		{"0 . 0 . 0 . 0", false, ""},
	}

	for i := range testCases {
		tc := &testCases[i]
		var f flag.Value = &IP{}
		err := f.Set(tc.input)
		if err != nil && tc.success == true {
			t.Errorf("expected success, got %q", err)
			continue
		} else if err == nil && tc.success == false {
			t.Errorf("expected failure")
			continue
		} else if tc.success {
			if f.String() != tc.expected {
				t.Errorf("expected %q, got %q", tc.expected, f.String())
			}
		}
	}
}
开发者ID:ericcapricorn,项目名称:kubernetes,代码行数:38,代码来源:net_test.go


示例6: Var

func (self *flagDummy) Var(value goflag.Value, name string, usage string) {
	flag := flag{name, value.String(), usage}
	*self.list = append(*self.list, flag)
}
开发者ID:yonglehou,项目名称:golang-distributed-filesystem,代码行数:4,代码来源:command.go


示例7: main

func main() {
	var flag flag.Value = new(MyFlag)
	flag.Set("joe")
}
开发者ID:zouyx,项目名称:learnGo,代码行数:4,代码来源:8.4.2.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang fmt.Errorf函数代码示例发布时间:2022-05-24
下一篇:
Golang flag.FlagSet类代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap