好的,我在使用标志时遇到了麻烦。我认为我目前在正确的轨道上,但是如果我输入“go run *.go printrepeater 3 --slow”,我的 PrintRepeater 程序中的 println 将输出 true,但是如果我输入“go run *.go printrepeater 3 slow”我发烧了。testCli.gopackage 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.gopackage mainimport "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") }}
1 回答
茅侃侃
TA贡献1842条经验 获得超21个赞
标志以 a 开头-
,这就是它们的定义方式。
当您使用时printrepeater 3 slow
,“slow”现在是一个额外的参数,并且不会影响slow
标志的状态。
- 1 回答
- 0 关注
- 142 浏览
添加回答
举报
0/150
提交
取消