gocron源碼中使用的是下面這個第三方包來實現的,下面就單獨的拿出來測試如下效果,和官方flag包差很少git
go get github.com/urfave/cligithub
package main import ( "fmt" "log" "os" "github.com/urfave/cli" ) func main() { app := cli.NewApp() var flags []cli.Flag flags = append(flags, cli.StringFlag{ Name: "host", Value: "0.0.0.0", Usage: "bind host", }, cli.IntFlag{ Name: "port,p", Value: 5090, Usage: "bind port", }, cli.StringFlag{ Name: "env,e", Value: "prod", Usage: "runtime environment, dev|test|prod", }) app.Flags = flags app.Action = func(c *cli.Context) error { fmt.Println("host:", c.String("host")) fmt.Println("port:", c.String("port")) fmt.Println("env:", c.String("env")) return nil } err := app.Run(os.Args) if err != nil { log.Fatal(err) } }