好的,我在使用標志時遇到了麻煩。我認為我目前在正確的軌道上,但是如果我輸入“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條經驗 獲得超22個贊
標志以 a 開頭-,這就是它們的定義方式。
當您使用時printrepeater 3 slow,“slow”現在是一個額外的參數,并且不會影響slow標志的狀態。
- 1 回答
- 0 關注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消
