森林海
我认为您可能没有正确定义标志,请参阅我编写的这个示例:package mainimport ( "fmt" "os" "github.com/codegangsta/cli")func main() { app := cli.NewApp() app.Name = "so-example" app.Usage = "Demonstrate CLI usage" app.Commands = []cli.Command{ { Name: "one", Usage: "first thing", Flags: []cli.Flag{ cli.StringFlag{ Name: "test", Value: "foobar", Usage: "something cool", }, }, Action: func(c *cli.Context) { fmt.Println("completed task", c.Command.Name, " with args ", c.Args()) if (c.String("test") != "foobar") { fmt.Println("Where'd foobar go?") } }, }, { Name: "two", Usage: "second thing", Flags: []cli.Flag{ cli.StringFlag{ Name: "test", Value: "foobar", Usage: "something cool", }, }, Action: func(c *cli.Context) { fmt.Println("completed task", c.Command.Name, " with args ", c.Args()) fmt.Println("Testing value:", c.String("test")) if (c.String("test") != "foobar") { fmt.Println("Where'd foobar go?") } }, }, } app.Run(os.Args)}快速示例:无旗$ ./sandbox two foo bar hello worldcompleted task two with args [foo bar hello world]Testing value: foobar带旗$ ./sandbox two foo bar hello world --test "see"completed task two with args [foo bar hello world]Testing value: seeWhere'd foobar go?