一、urfave/cli
urfave/cli 是一个声明性的、简单、快速且有趣的包,用于用 Go 构建命令行工具。
二、快速使用
2.1 引入依赖
go get github.com/urfave/cli/v2
2.2 demo
package mainimport ("fmt""log""os""github.com/urfave/cli/v2"
)func main() {// 创建 urfave app 实例app := &cli.App{// 设置命令Commands: []*cli.Command{{Name: "add",Aliases: []string{"a"},Usage: "add a task to the list",Flags: []cli.Flag{&cli.StringFlag{Name: "type",Value: "daily",Usage: "add eat --type=daily",},},Action: func(cCtx *cli.Context) error {fmt.Println("added task: ", cCtx.Args().First(), "type=", cCtx.String("type"))return nil},},},}// 启动命令if err := app.Run(os.Args); err != nil {log.Fatal(err)}
}