[Go] gocron source read - achieved through command-line parameters to obtain and manage third-party package cli

gocron source is used in the following third-party packages to achieve, to separate out the following to test the effect, and the official flag bag almost

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)
    }
}

Guess you like

Origin www.cnblogs.com/taoshihan/p/11872244.html