go之命令行工具urfave-cli

一、urfave/cli

urfave/cli 是一个声明性的、简单、快速且有趣的包,用于用 Go 构建命令行工具。

二、快速使用

2.1 引入依赖
bash 复制代码
go get github.com/urfave/cli/v2
2.2 demo
Go 复制代码
package main

import (
    "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)
	}
}
2.3 命令行使用
相关推荐
yhole1 分钟前
springboot三层架构详细讲解
spring boot·后端·架构
香香甜甜的辣椒炒肉8 分钟前
Spring(1)基本概念+开发的基本步骤
java·后端·spring
白毛大侠1 小时前
Go Goroutine 与用户态是进程级
开发语言·后端·golang
ForteScarlet1 小时前
从 Kotlin 编译器 API 的变化开始: 2.3.20
android·开发语言·后端·ios·开源·kotlin
大阿明1 小时前
SpringBoot - Cookie & Session 用户登录及登录状态保持功能实现
java·spring boot·后端
Binary-Jeff1 小时前
Spring 创建 Bean 的关键流程
java·开发语言·前端·spring boot·后端·spring·学习方法
TlYf NTLE2 小时前
Spring Boot3.3.X整合Mybatis-Plus
spring boot·后端·mybatis
SHoM SSER2 小时前
Spring Boot性能提升的核武器,速度提升500%!
java·spring boot·后端
weixin_425023002 小时前
Spring Boot 2.7 + JDK8 集成 Knife4j 4.1.0 教程(仅展示带注解接口)
java·spring boot·后端
woniu_maggie2 小时前
SAP报错:表 T030 中由于读入错误无法确定费用科目
后端