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


Golang flag.NFlag函数代码示例

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


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


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