gocron源码中使用的是下面这个第三方包来实现的,下面就单独的拿出来测试以下效果,和官方flag包差不多

go get github.com/urfave/cli

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)
    }
}
12-28 15:50