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


Golang go-flags.NewParser函数代码示例

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


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

示例1: main

func main() {

	flags := &server.Options{}

	parser := goflags.NewParser(flags, goflags.HelpFlag|goflags.PrintErrors)

	// parse flags
	args, err := parser.Parse()
	if err != nil {
		// assert the err to be a flags.Error
		flagError, ok := err.(*goflags.Error)
		if !ok {
			// not a flags error
			os.Exit(1)
		}
		if flagError.Type == goflags.ErrHelp {
			// exitcode 0 when user asked for help
			os.Exit(0)
		}
		if flagError.Type == goflags.ErrUnknownFlag {
			fmt.Println("run with --help to view available options")
		}
		os.Exit(1)
	}

	// error on left-over arguments
	if len(args) > 0 {
		fmt.Printf("unexpected arguments: %s\n", args)
		os.Exit(0)
	}

	s := server.New(flags)
	s.Run(nil)
}
开发者ID:GeenPeil,项目名称:teken,代码行数:34,代码来源:main.go

示例2: main

func main() {
	swaggerSpec, err := spec.New(restapi.SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	api := operations.NewPetstoreAPI(swaggerSpec)
	server := restapi.NewServer(api)
	defer server.Shutdown()

	parser := flags.NewParser(server, flags.Default)
	parser.ShortDescription = `Swagger Petstore`
	parser.LongDescription = `This is a sample server Petstore server.

[Learn about Swagger](http://swagger.wordnik.com) or join the IRC channel '#swagger' on irc.freenode.net.

For this sample, you can use the api key 'special-key' to test the authorization filters
`

	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		log.Fatalln(err)
	}

	if err := server.Serve(); err != nil {
		log.Fatalln(err)
	}
}
开发者ID:jak-atx,项目名称:vic,代码行数:31,代码来源:main.go

示例3: HyperCmdAttach

func (cli *HyperClient) HyperCmdAttach(args ...string) error {
	var parser = gflag.NewParser(nil, gflag.Default)
	parser.Usage = "attach CONTAINER\n\nAttach to the tty of a specified container in a pod"
	args, err := parser.ParseArgs(args)
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	if len(args) == 0 {
		return fmt.Errorf("Can not accept the 'attach' command without Container ID!")
	}
	var (
		podId       = args[0]
		containerId = args[0]
		tty         bool
	)

	if strings.Contains(podId, "pod-") {
		pod, err := cli.client.GetPodInfo(podId)
		if err != nil {
			return err
		}

		if len(pod.Spec.Containers) == 0 {
			return fmt.Errorf("failed to get container from %s", podId)
		}

		c := pod.Spec.Containers[0]

		containerId = c.ContainerID
		tty = c.Tty
	} else {
		c, err := cli.client.GetContainerInfo(containerId)
		if err != nil {
			return err
		}

		podId = c.PodID
		containerId = c.Container.ContainerID
		tty = c.Container.Tty
	}

	if tty {
		oldState, err := term.SetRawTerminal(cli.inFd)
		if err != nil {
			return err
		}
		defer term.RestoreTerminal(cli.inFd, oldState)
		cli.monitorTtySize(containerId, "")
	}

	if err := cli.client.Attach(containerId, tty, cli.in, cli.out, cli.err); err != nil {
		return err
	}

	return cli.client.GetExitCode(containerId, "")
}
开发者ID:juito,项目名称:hyper,代码行数:60,代码来源:attach.go

示例4: HyperCmdPull

func (cli *HyperClient) HyperCmdPull(args ...string) error {
	// we need to get the image name which will be used to create a container
	if len(args) == 0 {
		return fmt.Errorf("\"pull\" requires a minimum of 1 argument, please provide the image name.")
	}
	var parser = gflag.NewParser(nil, gflag.Default)
	parser.Usage = "pull IMAGE\n\npull an image from a Docker registry server"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	imageName := args[1]
	v := url.Values{}
	v.Set("imageName", imageName)
	err = cli.stream("POST", "/image/create?"+v.Encode(), nil, nil, nil)
	if err != nil {
		return err
	}

	return nil
}
开发者ID:huangqg,项目名称:hyper,代码行数:25,代码来源:pull.go

示例5: parseFlags

func parseFlags() ([]string, error) {
	p := flags.NewParser(&opts, flags.Default)

	// display nice usage message
	p.Usage = "[OPTIONS]... COMMAND\n\nSafely wrap execution of COMMAND in e.g. a cron job"

	args, err := p.Parse()
	if err != nil {
		// --help is not an error
		if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
			return nil, nil
		}
		return nil, err
	}

	if len(args) < 1 {
		return nil, &FlagConstraintError{Constraint: "no command to execute"}
	}

	if err := validateOptionConstraints(); err != nil {
		return nil, err
	}

	return args, nil
}
开发者ID:Jimdo,项目名称:periodicnoise,代码行数:25,代码来源:flags.go

示例6: main

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	// command line flags
	var opts struct {
		BindAddress string `long:"listen" short:"l" default:"0.0.0.0:8000" description:"Address:Port to bind to for HTTP"`
	}

	// parse said flags
	parser := flags.NewParser(&opts, flags.Default)
	parser.Usage = "[OPTIONS] DIR"
	args, err := parser.Parse()
	if err != nil {
		if e, ok := err.(*flags.Error); ok {
			if e.Type == flags.ErrHelp {
				os.Exit(0)
			}
		}
		os.Exit(1)
	}

	var dirname string
	alen := len(args)
	switch {
	case alen < 1:
		dirname = "."
	case alen == 1:
		dirname = args[0]
	case alen > 1:
		log.Fatal("Too many arguments")
	}

	panic(http.ListenAndServe(opts.BindAddress, http.FileServer(http.Dir(dirname))))
}
开发者ID:r38y,项目名称:go-camo,代码行数:34,代码来源:main.go

示例7: main

func main() {

	parser := flags.NewParser(&opts, flags.Default)
	parser.Name = "githubpubkey"
	parser.Usage = "[-h] [-v] username"

	args, err := parser.Parse()

	if err != nil {
		os.Exit(0)
	}

	if opts.Version {
		fmt.Print("Version " + version)
		os.Exit(0)
	}

	if len(args) == 0 {
		parser.WriteHelp(os.Stdout)
		fmt.Print("\n")
		os.Exit(0)
	}

	var wg sync.WaitGroup

	for _, username := range args {
		wg.Add(1)
		go func(username string) {
			keyPrint("https://github.com/" + username + ".keys")
			wg.Done()
		}(username)
	}

	wg.Wait()
}
开发者ID:ngc224,项目名称:githubpubkey,代码行数:35,代码来源:githubpubkey.go

示例8: main

func main() {
	swaggerSpec, err := spec.New(restapi.SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	api := operations.NewTaskTrackerAPI(swaggerSpec)
	server := restapi.NewServer(api)
	defer server.Shutdown()

	parser := flags.NewParser(server, flags.Default)
	parser.ShortDescription = `Issue Tracker API`
	parser.LongDescription = `This application implements a very simple issue tracker.
It's implemented as an API which is described by this swagger spec document.

The go-swagger project uses this specification to test the code generation.
This document contains all possible values for a swagger definition.
This means that it exercises the framework relatively well.
`

	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		log.Fatalln(err)
	}

	if err := server.Serve(); err != nil {
		log.Fatalln(err)
	}
}
开发者ID:vmware,项目名称:vic,代码行数:32,代码来源:main.go

示例9: checkArgs

// コマンドライン引数チェック用関数
func checkArgs() *options.Options {
	var opts options.Options
	parser := flags.NewParser(&opts, flags.Default)
	parser.Name = "lgb"
	parser.Usage = "[OPTIONS]"
	_, err := parser.Parse()
	if err != nil {
		os.Exit(1)
	}

	if opts.ConsumerKey == "" || opts.ConsumerSecret == "" || opts.AccessToken == "" || opts.AccessTokenSecret == "" {
		parser.WriteHelp(os.Stdout)
		os.Exit(1)
	}

	if opts.Width < 1 || opts.Height < 1 {
		parser.WriteHelp(os.Stdout)
		os.Exit(1)
	}

	if opts.AliveRate < 1 || opts.AliveRate > 100 || opts.MutRate < 0 || opts.MutRate > 100 || opts.Generation < 0 {
		parser.WriteHelp(os.Stdout)
		os.Exit(1)
	}

	return &opts
}
开发者ID:Rompei,项目名称:lgb,代码行数:28,代码来源:main.go

示例10: main

func main() {
	swaggerSpec, err := spec.New(restapi.SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	api := operations.NewTodoListAPI(swaggerSpec)
	server := restapi.NewServer(api)
	defer server.Shutdown()

	parser := flags.NewParser(server, flags.Default)
	parser.ShortDescription = `Simple To Do List API`
	parser.LongDescription = swaggerSpec.Spec().Info.Description

	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		log.Fatalln(err)
	}

	if err := server.Serve(); err != nil {
		log.Fatalln(err)
	}
}
开发者ID:jak-atx,项目名称:vic,代码行数:26,代码来源:main.go

示例11: HyperCmdLoad

func (cli *HyperClient) HyperCmdLoad(args ...string) error {
	var opts struct {
		Input string `short:"i" long:"input" value-name:"\"\"" description:"Read from a tar archive file, instead of STDIN"`
	}

	var parser = gflag.NewParser(&opts, gflag.Default|gflag.IgnoreUnknown)
	parser.Usage = "load [OPTIONS]\n\nLoad a image from STDIN or tar archive file"
	args, err := parser.ParseArgs(args)
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	var input io.Reader = cli.in
	if opts.Input != "" {
		file, err := os.Open(opts.Input)
		if err != nil {
			return err
		}
		defer file.Close()
		input = file
	}
	output, ctype, err := cli.client.Load(input)
	if err != nil {
		return err
	}
	return cli.readStreamOutput(output, ctype, false, cli.out, cli.err)
}
开发者ID:juito,项目名称:hyper,代码行数:30,代码来源:load.go

示例12: HyperCmdLoad

func (cli *HyperClient) HyperCmdLoad(args ...string) error {
	var opts struct {
		Input string `short:"i" long:"input" value-name:"\"\"" description:"Read from a tar archive file, instead of STDIN"`
	}

	var parser = gflag.NewParser(&opts, gflag.Default|gflag.IgnoreUnknown)
	parser.Usage = "load [OPTIONS]\n\nLoad a image from STDIN or tar archive file"
	args, err := parser.ParseArgs(args)
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	var input io.Reader = cli.in
	if opts.Input != "" {
		file, err := os.Open(opts.Input)
		if err != nil {
			return err
		}
		defer file.Close()
		input = file
	}

	headers := map[string][]string{"Content-Type": {"application/x-tar"}}
	return cli.stream("POST", "/image/load", input, cli.out, headers)
}
开发者ID:ZJU-SEL,项目名称:hyper,代码行数:28,代码来源:load.go

示例13: HyperCmdStop

func (cli *HyperClient) HyperCmdStop(args ...string) error {

	var opts struct {
		//		Novm        bool     `long:"onlypod" default:"false" value-name:"false" description:"Stop a Pod, but left the VM running"`
	}
	var parser = gflag.NewParser(&opts, gflag.Default)
	parser.Usage = "stop POD_ID\n\nstop a running pod"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	if len(args) == 1 {
		return fmt.Errorf("\"stop\" requires a minimum of 1 argument, please provide POD ID.\n")
	}

	podID := args[1]
	stopVm := "yes"
	if false {
		stopVm = "no"
	}
	code, cause, err := cli.StopPod(podID, stopVm)
	if err != nil {
		return err
	}
	if code != types.E_POD_STOPPED && code != types.E_VM_SHUTDOWN {
		return fmt.Errorf("Error code is %d, cause is %s", code, cause)
	}
	fmt.Printf("Success to shutdown the POD: %s!\n", podID)
	return nil
}
开发者ID:carriercomm,项目名称:hyper,代码行数:34,代码来源:stop.go

示例14: HyperCmdPod

// We need to process the POD json data with the given file
func (cli *HyperClient) HyperCmdPod(args ...string) error {
	t1 := time.Now()
	var parser = gflag.NewParser(nil, gflag.Default)
	parser.Usage = "pod POD_FILE\n\nCreate a pod, initialize a pod and run it"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	if len(args) < 2 {
		return fmt.Errorf("\"pod\" requires a minimum of 1 argument, please provide POD spec file.\n")
	}
	jsonFile := args[1]
	if _, err := os.Stat(jsonFile); err != nil {
		return err
	}

	jsonbody, err := ioutil.ReadFile(jsonFile)
	if err != nil {
		return err
	}
	podId, err := cli.RunPod(string(jsonbody), false)
	if err != nil {
		return err
	}
	fmt.Fprintf(cli.out, "POD id is %s\n", podId)
	t2 := time.Now()
	fmt.Fprintf(cli.out, "Time to run a POD is %d ms\n", (t2.UnixNano()-t1.UnixNano())/1000000)

	return nil
}
开发者ID:m1911,项目名称:hyper,代码行数:35,代码来源:pod.go

示例15: HyperCmdUnpause

func (cli *HyperClient) HyperCmdUnpause(args ...string) error {
	var parser = gflag.NewParser(nil, gflag.Default|gflag.IgnoreUnknown)
	parser.Usage = "unpause Pod\n\nUnpause the pod"
	args, err := parser.ParseArgs(args)
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	if len(args) == 0 {
		return fmt.Errorf("Can not accept the 'unpause' command without Pod ID!")
	}

	v := url.Values{}
	v.Set("podId", args[0])

	body, _, err := readBody(cli.call("POST", "/pod/unpause?"+v.Encode(), nil, nil))
	if err != nil {
		return err
	}
	out := engine.NewOutput()
	if _, err = out.AddEnv(); err != nil {
		return err
	}

	if _, err := out.Write(body); err != nil {
		return err
	}
	out.Close()
	return nil
}
开发者ID:ZJU-SEL,项目名称:hyper,代码行数:33,代码来源:unpause.go


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