当我在标志上从“--”更改为 no-- 时得到不同的输出

Getting different outputs when I change from "--" to no -- on flag

好的,我在使用标志时遇到了问题。我认为我目前走在正确的轨道上,但如果我键入 "go run *.go printrepeater 3 --slow",我的 PrintRepeater 程序中的 println 将输出 true,但如果我键入 "go run *.go printrepeater 3 slow",我会得到 flase。

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")
    }
}

标志以 - 开头,这就是它们的定义方式。

当您使用 printrepeater 3 slow 时,"slow" 现在是一个额外的参数,并且不会影响 slow 标志的状态。