好的,我在使用标志时遇到了麻烦。我认为我目前在正确的轨道上,但是如果我输入“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")
}
}
茅侃侃
相关分类