英文:
Getting different outputs when I change from "--" to no -- on flag
问题
好的,以下是翻译好的内容:
好的,我在处理标志(flags)时遇到了问题。我认为我目前走在正确的轨道上,但是如果我在我的PrintRepeater程序中输入"go run *.go printrepeater 3 --slow",我的println语句将输出true,但是如果我输入"go run *.go printrepeater 3 slow",我得到的是false。
testCli.go
package main
import (
"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")
}
}
英文:
Ok, I have having trouble with flags. I think I currently am on the right track but my println in my PrintRepeater program will output a true if I type "go run *.go printrepeater 3 --slow", but if I type "go run *.go printrepeater 3 slow" I get flase.
testCli.go
package main
import (
"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")
}
}
答案1
得分: 1
标志以“-”开头,这就是它们的定义方式。
当你使用printrepeater 3 slow
时,“slow”现在是一个额外的参数,不会影响“slow”标志的状态。
英文:
Flags start with a -
, that's just how they are defined.
When you use printrepeater 3 slow
, "slow" is now an extra argument, and doesn't affect the state of the slow
flag.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论