本文整理汇总了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)
}
示例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)
}
}
示例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, "")
}
示例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
}
示例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
}
示例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))))
}
示例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()
}
示例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)
}
}
示例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
}
示例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)
}
}
示例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)
}
示例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)
}
示例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
}
示例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
}
示例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
}