golang三方库之urfave/cli
1. urfave/cli
1.1. 安装 & import
安装:
// 使用v1版本
$ go get github.com/urfave/cli
// 使用v2版本
$ go get github.com/urfave/cli/v2
引用:
// 使用v1版本
import (
"github.com/urfave/cli"
)
// 使用v2版本
import (
"github.com/urfave/cli/v2" // imports as package "cli"
)
urfave/cli共有2个版本,后续章节围绕
urfave/cli/v2
进行学习
1.2. 使用案例
(1)普通接收参数 args
通过cCtx.Args().Get(0)
获取
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Action: func(cCtx *cli.Context) error {
fmt.Printf("Hello %q", cCtx.Args().Get(0))
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
调用:
$ go run main.go xiaoming
Hello xiaoming
(2)Flag增加选项参数
,可加默认值
不能通过cCtx.Args().Get(0)
获取Flag选项 & 参数值
package main
import (
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{ //string类型
Name: "lang", //选项
Aliases: []string{"l"}, //选项别名
Value: "english", //默认值
Usage: "language for the greeting",
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
调用:
$ go run learn_cli.go -h
NAME:
learnCli - A new cli application
USAGE:
learnCli [global options] command [command options] [arguments...]
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--lang value, -l value language for the greeting (default: "english") //成功展示
--help, -h show help (default: false)
(3)command,增加命令;subcommand,增加子命令
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "learnCli",
Commands: []*cli.Command{ //命令
{
Name: "add",
Aliases: []string{"a"},
Usage: "add a task to the list",
Action: func(cCtx *cli.Context) error {
fmt.Println("added task: ", cCtx.Args().First())
return nil
},
},
{
Name: "template",
Aliases: []string{"t"},
Usage: "options for task templates",
Subcommands: []*cli.Command{ //子命令
{
Name: "add",
Usage: "add a new template",
Action: func(cCtx *cli.Context) error {
fmt.Println("new task template: ", cCtx.Args().First())
return nil
},
},
{
Name: "remove",
Usage: "remove an existing template",
Action: func(cCtx *cli.Context) error {
fmt.Println("removed task template: ", cCtx.Args().First())
return nil
},
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
调用:
- 展示command
$ go run learn_cli.go -h
NAME:
learnCli - A new cli application
USAGE:
learnCli [global options] command [command options] [arguments...]
COMMANDS:
add, a add a task to the list //成功展示command
template, t options for task templates //成功展示command
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--help, -h show help (default: false)
- 展示subcommand
$ go run learn_cli.go template -h
NAME:
learnCli template - options for task templates
USAGE:
learnCli template command [command options] [arguments...]
COMMANDS:
add add a new template //成功展示subcommand
remove remove an existing template //成功展示subcommand
help, h Shows a list of commands or help for one command
OPTIONS:
--help, -h show help (default: false)
(4)添加Version
- 方式一:默认情况下,需要使用
cli.VersionFlag
添加-v/--version
package main
import (
"os"
"github.com/urfave/cli/v2"
)
func main() {
cli.VersionFlag = &cli.BoolFlag{ //设置version
Name: "version",
Aliases: []string{"v"},
Usage: "print only the version",
}
app := &cli.App{
Name: "partay",
Version: "v19.99.0",
}
app.Run(os.Args)
}
查看帮助文档:
$ go run learn_cli.go -h
NAME:
partay - A new cli application
USAGE:
partay [global options] command [command options] [arguments...]
VERSION:
v19.99.0
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--help, -h show help (default: false)
--version, -v print only the version (default: false) //成功展示version
$ go run learn_cli.go -v //调用version
partay version v19.99.0
- 方式二:使用
cli.VersionPrinter
添加version选项 (推荐使用
)
package main
import (
"fmt"
"os"
"github.com/urfave/cli/v2"
)
var (
Revision = "fafafaf"
)
func main() {
cli.VersionPrinter = func(cCtx *cli.Context) {
fmt.Printf("version=%s revision=%s\n", cCtx.App.Version, Revision)
}
app := &cli.App{
Name: "partay",
Version: "v19.99.0",
}
app.Run(os.Args)
}
命令行调用:
$ go run learn_cli.go -h //查看帮助文档
NAME:
partay - A new cli application
USAGE:
partay [global options] command [command options] [arguments...]
VERSION:
v19.99.0
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--help, -h show help (default: false)
--version, -v print the version (default: false) //成功添加
$ go run learn_cli.go -v //调用version
version=v19.99.0 revision=fafafaf
(5)Combining short options:结合短选项
假设您希望用户能够将选项与他们的短名称组合在一起。这可以通过在应用程序配置中使用UseShortOptionHandling
bool来完成,或者通过将它附加到命令配置中来完成单个命令。例如:
app := &cli.App{}
app.UseShortOptionHandling = true //设置UseShortOptionHandling 参数为 true
app.Commands = []*cli.Command{
{
Name: "short",
Usage: "complete a task on the list",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "serve", Aliases: []string{"s"}},
&cli.BoolFlag{Name: "option", Aliases: []string{"o"}},
&cli.StringFlag{Name: "message", Aliases: []string{"m"}},
},
Action: func(c *cli.Context) error {
fmt.Println("serve:", c.Bool("serve"))
fmt.Println("option:", c.Bool("option"))
fmt.Println("message:", c.String("message"))
return nil
},
},
}
//他们还可以组合起来使用,例如:
short -som "Some message"