猿问

当我从 "--" 变为 no -- on flag 时获得不同的输出

好的,我在使用标志时遇到了麻烦。我认为我目前在正确的轨道上,但是如果我输入“go run *.go printrepeater 3 --slow”,我的 PrintRepeater 程序中的 println 将输出 true,但是如果我输入“go run *.go printrepeater 3 slow”我发烧了。


testCli.go


package main

导入(“github.com/codegangsta/cli”“os”)


func main() {

app := cli.NewApp()

app.Name = "Learn CLI"

app.Usage = "basic things in cli"

/*  app.Flags = []gangstaCli.Flag{

    gangstaCli.StringFlag{

        Name: "s",

        //Value:  "y",

        Usage: "slowing down",

    },

    }*/


  //    app.Flags = []cli.Flag{

  //cli.StringFlag{"slow", "yes", "for when you have too much time", ""},

 // }


 app.Commands = []cli.Command{

    {

        Name:   "countup",

        Usage:  "counting up",

        Action: PrintRepeater,

    },


    {

        Name:   "countdown",

        Usage:  "counting down",

        Action: GoDown,

    },


    {

        Name:  "printrepeater",

        Usage: "prints hello x number of times",

        Flags: []cli.Flag{

            cli.BoolFlag{

                Name:  "slow",

                Usage: "to slow things down by a certian amount",

            },

        },

        Action: PrintRepeater,

    },

}


app.Run(os.Args)

}

PrintRepeater.go


package main


import "github.com/codegangsta/cli"

import "strconv"


func PrintRepeater(c *cli.Context) {

    println(c.Bool("slow"))


    i1 := c.Args()[0]

    i2, err := strconv.Atoi(i1)

    if err != nil {

        println(err)

    }

    for i := i2; i >= 1; i-- {

        println("hello")

    }

}


鸿蒙传说
浏览 143回答 1
1回答

茅侃侃

标志以 a 开头-,这就是它们的定义方式。当您使用时printrepeater 3 slow,“slow”现在是一个额外的参数,并且不会影响slow标志的状态。
随时随地看视频慕课网APP

相关分类

Go
我要回答