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

Golang flag.NFlag函数代码示例

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

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



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

示例1: main

func main() {
	// Parse the command line flags.
	hexPtr := flag.String("hex", "", "a hex value to convert to RGB")
	flag.Var(&rgbflag, "rgb", "an RGB value to convert to hex")
	flag.Parse()

	// If neither flag is provided, print usage and exit.
	if flag.NFlag() == 0 || flag.NFlag() == 2 {
		fmt.Println("Usage: rgbhex [-hex=ABCDEF | -rgb=123,234,100]")
		os.Exit(1)
	}

	if *hexPtr != "" {
		// Convert the provided hex value into an integer.
		hexVal, err := strconv.ParseInt(*hexPtr, 16, 32)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		fmt.Println(HexToRGB(hexVal))
	} else {
		// Convert an RGB value to hex.
		fmt.Println(RGBToHex(&rgbflag))
	}
}
开发者ID:RocketDonkey,项目名称:random,代码行数:25,代码来源:rgbhex.go


示例2: main

func main() {
	// Arguments
	fileName := flag.String("c", "config.json", "config file")
	debug := flag.Bool("d", false, "debug mode")
	vers := flag.Bool("v", false, "version")
	flag.Parse()
	// Version
	if *vers {
		fmt.Fprintln(os.Stdout, version.Version)
		os.Exit(1)
	}
	// Logging
	log.SetName("banshee")
	if *debug {
		log.SetLevel(log.DEBUG)
	}
	log.Debug("using %s, max %d cpu", runtime.Version(), runtime.GOMAXPROCS(-1))
	// Config
	cfg := config.New()
	if flag.NFlag() == 0 || (flag.NFlag() == 1 && *debug == true) {
		log.Warn("no config file specified, using default..")
	} else {
		err := cfg.UpdateWithJSONFile(*fileName)
		if err != nil {
			log.Fatal("failed to load %s, %s", *fileName, err)
		}
	}
	// Storage
	options := &storage.Options{
		NumGrid: cfg.Period[0],
		GridLen: cfg.Period[1],
	}
	db, err := storage.Open(cfg.Storage.Path, options)
	if err != nil {
		log.Fatal("failed to open %s: %v", cfg.Storage.Path, err)
	}
	// Cleaner
	cleaner := cleaner.New(db, cfg.Period[0]*cfg.Period[1])
	go cleaner.Start()
	// Filter
	filter := filter.New()
	filter.Init(db)
	// Alerter
	alerter := alerter.New(cfg, db, filter)
	alerter.Start()
	// Webapp
	go webapp.Start(cfg, db)
	// Detector
	detector := detector.New(cfg, db, filter)
	detector.Out(alerter.In)
	detector.Start()
}
开发者ID:nrzipher,项目名称:banshee,代码行数:52,代码来源:main.go


示例3: main

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, usage)
		fmt.Fprintln(os.Stderr, "Flags:")
		flag.PrintDefaults()
		os.Exit(2)
	}

	flag.Parse()
	if (flag.NFlag() == 0 && flag.NArg() != 2) || (flag.NFlag() == 1 && flag.NArg() > 1) {
		flag.Usage()
	}

	addr := flag.Arg(0)
	if !strings.Contains(addr, ":") {
		addr = ":" + addr
	}

	// Server mode
	if *listen {
		l, err := net.Listen("tcp", addr)
		if err != nil {
			log.Fatal(err)
		}
		defer l.Close()

		log.Fatal(Serve(l))
	}

	// Client mode
	conn, err := Dial(addr)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	buf := []byte(flag.Arg(1))
	if _, err := conn.Write(buf); err != nil {
		log.Fatal(err)
	}

	n, err := conn.Read(buf)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s\n", buf[:n])
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:48,代码来源:main.go


示例4: main

func main() {
	defer glog.Flush()

	if flag.NFlag() == 0 {
		flag.PrintDefaults()
		return
	}
	if pv {
		printVersion()
		return
	}

	if len(listenArgs) == 0 {
		glog.Fatalln("no listen addr")
	}

	var wg sync.WaitGroup
	for _, args := range listenArgs {
		wg.Add(1)
		go func(arg Args) {
			defer wg.Done()
			listenAndServe(arg)
		}(args)
	}
	wg.Wait()
}
开发者ID:noscripter,项目名称:gost-1,代码行数:26,代码来源:main.go


示例5: main

func main() {
	flag.Parse()
	isPublic := !*gistPrivateFlag

	// if nothing was given write message
	if (flag.NFlag() == 0) && (len(flag.Args()) == 0) {
		fmt.Println("No arguments or files given!")
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(2)
	}

	token := Configuration.GetToken()

	if *listGistsFlag != "" {
		username := *listGistsFlag
		url := baseUrl + "users/" + username + "/gists"
		Gist.List(url)
	} else if *deleteGistFlag != "" {
		Gist.Delete(baseUrl, token, *deleteGistFlag)
	} else if *downloadGistFlag != "" {
		Gist.Download(baseUrl, token, *downloadGistFlag)
	} else {
		filesName := flag.Args()
		if len(filesName) == 0 && *updateGistFlag == "" {
			fmt.Println("No files given!")
			os.Exit(2)
		}
		if *updateGistFlag != "" {
			Gist.Update(baseUrl, token, filesName, *updateGistFlag, *gistDescriptionFlag, *openBrowserFlag)
		} else {
			Gist.Post(baseUrl, token, isPublic, filesName, *gistDescriptionFlag, *openBrowserFlag)
		}
	}
}
开发者ID:ramtiga,项目名称:gost,代码行数:35,代码来源:gost.go


示例6: init

func init() {
	var (
		configureFile string
		printVersion  bool
	)

	flag.StringVar(&configureFile, "C", "", "configure file")
	flag.Var(&options.ChainNodes, "F", "forward address, can make a forward chain")
	flag.Var(&options.ServeNodes, "L", "listen address, can listen on multiple ports")
	flag.BoolVar(&printVersion, "V", false, "print version")
	flag.Parse()

	if err := loadConfigureFile(configureFile); err != nil {
		glog.Fatal(err)
	}

	if glog.V(5) {
		http2.VerboseLogs = true
	}

	if flag.NFlag() == 0 {
		flag.PrintDefaults()
		return
	}

	if printVersion {
		fmt.Fprintf(os.Stderr, "GOST %s (%s)\n", gost.Version, runtime.Version())
		return
	}
}
开发者ID:guest6379,项目名称:gost,代码行数:30,代码来源:main.go


示例7: main

func main() {
	configFile := flag.String("config", "logstreamer.toml", "Heka Logstreamer configuration file")

	flag.Parse()

	if flag.NFlag() == 0 {
		flag.PrintDefaults()
		os.Exit(0)
	}

	fconfig := make(FileConfig)
	if _, err := toml.DecodeFile(*configFile, &fconfig); err != nil {
		log.Printf("Error decoding config file: %s", err)
		return
	}

	// Filter out logstream inputs
	inputs := make(map[string]toml.Primitive)
	for name, prim := range fconfig {
		basic := new(Basic)
		if name == "LogstreamerInput" {
			inputs[name] = prim
		} else if err := toml.PrimitiveDecode(prim, &basic); err == nil {
			if basic.PluginType == "LogstreamerInput" {
				inputs[name] = prim
			}
		}
	}

	// Go through the logstreams and parse their configs
	for name, prim := range inputs {
		parseConfig(name, prim)
	}
}
开发者ID:CodeSummer,项目名称:heka,代码行数:34,代码来源:main.go


示例8: main

func main() {

	flag.Parse()
	if flag.NFlag() < 1 {
		fmt.Fprintf(os.Stderr, "usage:%s -f <filename>\n", os.Args[0])
		fmt.Fprintf(os.Stderr, "-n  Show number lines.\n")
		os.Exit(1)
	}

	file, err := os.Open(*filename)
	if err != nil {
		log.Fatal("%s\n", err)
	}

	scanner := bufio.NewScanner(file)
	var lines int

	for scanner.Scan() {
		if *numbers {
			fmt.Fprintf(os.Stdout, "%d: %s\n", lines, scanner.Text())
			lines++
			continue
		}
		fmt.Fprintf(os.Stdout, "%s\n", scanner.Text())
	}
}
开发者ID:sgmac,项目名称:snippets,代码行数:26,代码来源:numlines.go


示例9: main

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()
	if flag.NFlag() != 3 {
		fmt.Printf("usage: tcpip_proxy -host target_host -port target_port -listen_post=local_port\n")
		flag.PrintDefaults()
		os.Exit(1)
	}
	target := net.JoinHostPort(*host, *port)
	fmt.Printf("Start listening on port %s and forwarding data to %s\n", *listen_port, target)
	ln, err := net.Listen("tcp", ":"+*listen_port)
	if err != nil {
		fmt.Printf("Unable to start listener, %v\n", err)
		os.Exit(1)
	}
	conn_n := 1
	for {
		if conn, err := ln.Accept(); err == nil {
			go process_connection(conn, conn_n, target)
			conn_n += 1
		} else {
			fmt.Printf("Accept failed, %v\n", err)
		}
	}
}
开发者ID:vidmantas,项目名称:ruby-rails-workshops,代码行数:25,代码来源:tcpip_proxy.go


示例10: main

func main() {
	flag.Parse()
	if flag.NFlag() == 0 {
		fmt.Printf("no bottle given. usage:\n")
		flag.PrintDefaults()
		os.Exit(1)
	}
	fmt.Printf("=>> Start to uninstall gecko...\n")
	out, err := exec.Command("/opt/cxoffice/bin/wine", "--bottle="+bottle, "--", WineUninstallerPath, "--list").Output()
	if err != nil {
		fmt.Printf("ERR %s\n", err.Error())
	}
	arr := strings.Split(string(out), "\n")
	var counts int = 0
	for _, v := range arr {
		if len(v) == 0 {
			continue
		}
		s := strings.Split(v, "|||")
		if strings.HasPrefix(s[1], WeWantToRemove) {
			err := exec.Command("/opt/cxoffice/bin/wine", "--bottle="+bottle, "--", WineUninstallerPath, "--remove", s[0]).Run()
			if err != nil {
				fmt.Printf("ERR %s\n", err.Error())
			} else {
				counts++
			}
			fmt.Printf("<==done.\n")
		}
	}
	fmt.Printf("<<= %d uninstalled.\n", counts)
}
开发者ID:phcole,项目名称:scripts,代码行数:31,代码来源:gecko-cleaner.go


示例11: initConfig

func initConfig() {
	// Config parsing.
	if flag.NFlag() == 0 || (flag.NFlag() == 1 && *debug) { // Case ./program [-d]
		log.Warnf("no config specified, using default..")
	} else { // Case ./program -c filename
		err := cfg.UpdateWithYamlFile(*fileName)
		if err != nil {
			log.Fatalf("failed to load %s, %s", *fileName, err)
		}
	}
	// Config validation.
	err := cfg.Validate()
	if err != nil {
		log.Fatalf("config: %s", err)
	}
}
开发者ID:yujinqiu,项目名称:banshee,代码行数:16,代码来源:main.go


示例12: main

func main() {

	flag.Parse()

	if flag.NFlag() == 0 {
		flag.PrintDefaults()
	}

	if _, err := os.Stat(*filePath); os.IsNotExist(err) {
		//panic("File doesn't exist")
		fmt.Println("File doesn't exist")
		os.Exit(-1)
	}

	// Take all the necessary data
	appToken := os.Getenv("APP_TOKEN")
	appSecret := os.Getenv("APP_SECRET")
	accessToken := os.Getenv("ACCESS_TOKEN")
	accessSecret := os.Getenv("ACCESS_SECRET")

	// Create the client
	client, _ := copy.NewDefaultClient(appToken, appSecret, accessToken, accessSecret)
	fs := copy.NewFileService(client)
	fmt.Println(fs.UploadFile(*filePath, *uploadPath, true))
	os.Exit(0)
}
开发者ID:bancek,项目名称:go-copy,代码行数:26,代码来源:upload_example.go


示例13: main

func main() {
	to := flag.String("t", "", "destination Internet mail address")
	from := flag.String("f", "", "the sender's GMail address")
	pwd := flag.String("p", "", "the sender's password")
	subject := flag.String("s", "", "subject line of email")
	msg := flag.String("m", "", "a one-line email message")
	flag.Usage = func() {
		fmt.Printf("Syntax:\n\tGSend [flags]\nwhere flags are:\n")
		flag.PrintDefaults()
	}

	fmt.Printf("GSend v 1.01 by Jim Lawless\n")

	flag.Parse()

	if flag.NFlag() != 5 {
		flag.Usage()
		return
	}

	body := "To: " + *to + "\r\nSubject: " +
		*subject + "\r\n\r\n" + *msg
	auth := smtp.PlainAuth("", *from, *pwd, "smtp.gmail.com")
	err := smtp.SendMail("smtp.gmail.com:587", auth, *from,
		[]string{*to}, []byte(body))
	if err != nil {
		log.Fatal(err)
	}
}
开发者ID:samuelyao314,项目名称:mygo,代码行数:29,代码来源:gsend.go


示例14: main

func main() {
	flag.Parse()

	if flagOne == "" {
		fmt.Fprintf(os.Stdout, "Usage of cody.guo ok %s:\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(1)
	}

	fmt.Println("参数数量:", flag.NFlag())
	oneFlag := flag.Lookup("one")

	fmt.Println(oneFlag.Name, oneFlag.Value)

	// fmt.Println(len(os.Args))

	if debug {
		fmt.Println("debug is on.")
	} else {
		fmt.Println("debug is off.")
	}

	fmt.Println(flagOne)

	debugFlag := flag.Lookup("d")
	fmt.Println(debugFlag.Name, debugFlag.Value)

}
开发者ID:CodyGuo,项目名称:Go-Cody,代码行数:28,代码来源:main.go


示例15: main

func main() {
	detector := flag.Bool("detector", false, "start detector")
	// webapp := flag.Bool("webapp", false, "start webapp")
	// alerter := flag.Bool("alerter", false, "start alerter")
	fileName := flag.String("config", "", "config path")
	version := flag.Bool("version", false, "show version")
	flag.Parse()
	if *version {
		fmt.Fprintln(os.Stderr, VERSION)
		os.Exit(1)
	}
	if flag.NFlag() != 2 {
		flag.PrintDefaults()
		os.Exit(1)
	}
	cfg, err := config.NewWithJsonFile(*fileName)
	if err != nil {
		log.Fatalf("failed to read %s: %v", *fileName, err)
	}
	switch {
	case *detector:
		StartDetector(cfg.Detector)
	default:
		flag.PrintDefaults()
		os.Exit(1)
	}
}
开发者ID:hit9,项目名称:noise.old,代码行数:27,代码来源:main.go


示例16: main

func main() {
	// 定义一个string flag: -word, 其默认值为"foo"
	// flag.String函数返回值为指针类型
	wordPtr := flag.String("word", "foo", "intro: a string")
	numbPtr := flag.Int("num", 33, "an int")
	boolPtr := flag.Bool("fork", false, "an int")

	// 也可以使用变量
	// 定义一个flag -dada 存入svar中
	var svar string
	flag.StringVar(&svar, "dada", "dada", "a string var")

	// flag 定义完之后需要用flag.Parse()函数解析一下
	flag.Parse()

	fmt.Println("word:", *wordPtr)
	fmt.Println("num:", *numbPtr)
	fmt.Println("fork:", *boolPtr)
	fmt.Println("svar:", svar)
	fmt.Println("tail:", flag.Args())

	// 返回flag的个数
	fmt.Println("NFlag: ", flag.NFlag())
	// 去除flag之外的其他参数的个数
	fmt.Println("NArg: ", flag.NArg())

	if flag.NArg() > 0 {
		fmt.Println("args2:", flag.Args()[0])
	}

}
开发者ID:zykzhang,项目名称:practice,代码行数:31,代码来源:flag.go


示例17: main

func main() {
	defer glog.Flush()

	if flag.NFlag() == 0 {
		flag.PrintDefaults()
		return
	}
	if pv {
		fmt.Fprintln(os.Stderr, "gost", Version)
		return
	}

	listenArgs = parseArgs(listenAddr)
	forwardArgs = parseArgs(forwardAddr)

	if len(listenArgs) == 0 {
		glog.Exitln("no listen addr")
	}

	var wg sync.WaitGroup
	for _, args := range listenArgs {
		wg.Add(1)
		go func(arg Args) {
			defer wg.Done()
			listenAndServe(arg)
		}(args)
	}
	wg.Wait()
}
开发者ID:jim1568cas,项目名称:gost,代码行数:29,代码来源:main.go


示例18: main

func main() {
	n := new(counter)
	t := new(counter)

	flag.Parse()
	if flag.NFlag() == 0 {
		*lflag = true
		*wflag = true
		*cflag = true
	}

	if flag.NArg() == 0 {
		count(n, os.Stdin)
		report(n, "")
	}
	for i := 0; i < flag.NArg(); i++ {
		f, err := os.Open(flag.Arg(i))
		defer f.Close()
		if err != nil {
			fmt.Fprintf(os.Stderr, "wc: can't open %s: error %s\n", flag.Arg(i), err)
			os.Exit(1)
		}
		count(n, f)
		t.lines += n.lines
		t.words += n.words
		t.chars += n.chars
		t.errors += n.errors
		t.bytes += n.bytes
		report(n, flag.Arg(i))
	}
	if flag.NArg() > 1 {
		report(t, "total")
	}
}
开发者ID:beatgammit,项目名称:goblin,代码行数:34,代码来源:wc.go


示例19: main

func main() {
	help := flag.Bool("help", false, help_text)
	version := flag.Bool("version", false, version_text)
	flag.Parse()

	if *help {
		fmt.Println(help)
		os.Exit(0)
	}

	if *version {
		fmt.Println(version_text)
		os.Exit(0)
	}

	if flag.NArg() > 0 {
		fmt.Println(help)
		os.Exit(0)
	}

	if flag.NArg() == 0 && flag.NFlag() == 0 {

		var username string

		err := GetCurrentUser(&username)

		if err != nil {
			fmt.Fprintln(os.Stderr, err)
		}

		fmt.Println(username)

	}

}
开发者ID:ProhtMeyhet,项目名称:go-coreutils,代码行数:35,代码来源:logname.go


示例20: main

func main() {
	defer glog.Flush()

	if flag.NFlag() == 0 {
		flag.PrintDefaults()
		return
	}
	if pv {
		fmt.Println("gost", version, "git:", gitRev)
		return
	}

	listenArgs = parseArgs(listenAddr)
	forwardArgs = parseArgs(forwardAddr)

	if len(listenArgs) == 0 {
		glog.Exitln("no listen addr")
	}
	glog.Infof("gost %s(git: %s) started", version, gitRev)

	var wg sync.WaitGroup
	for _, args := range listenArgs {
		wg.Add(1)
		go func(arg Args) {
			defer wg.Done()
			listenAndServe(arg)
		}(args)
	}
	wg.Wait()
}
开发者ID:antmanler,项目名称:gost,代码行数:30,代码来源:main.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang flag.NewFlagSet函数代码示例发布时间:2022-05-24
下一篇:
Golang flag.NArg函数代码示例发布时间: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