简介
cli
一个简单、快速的命令行程序开发框架。
安装
go get -v github.com/urfave/cli
or
gopm get -v github.com/urfave/cli
示例
快速开始
Code:
package main
import (
"github.com/urfave/cli"
"os"
)
func main() {
_ = cli.NewApp().Run(os.Args)
}
Run:
go run main.go
Output:
NAME:
main - A new cli application
USAGE:
main [global options] command [command options] [arguments...]
VERSION:
0.0.0
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version
简单使用示例
Code:
package main
import (
"github.com/urfave/cli"
"os"
)
func main() {
app := cli.NewApp()
app.Name = "greet" // 应用名称
app.Usage = "say a greeting" // 应用功能说明
app.Action = func(c *cli.Context) error { // 应用执行函数
println("Greetings")
return nil
}
_ = app.Run(os.Args) // 运行应用
}
Run:
go run main
Output:
Greetings
Flag 使用使用
Code:
package main
import (
"fmt"
"github.com/urfave/cli"
"os"
)
func main() {
var name string
app := cli.NewApp()
app.Name = "hello" // 应用名称
app.Usage = "say hello" // 应用功能说明
app.Author = "arrows" // 应用作者
app.Email = "shangmlee@foxmail.com" // 邮箱
app.Version = "0.0.1" // 版本
app.Flags = []cli.Flag{
cli.StringFlag{
Name : "name, n", // 命令名称
Usage : "input your name", // 命令说明
Value : "Unknown", // 默认值
Destination: &name, // 赋值
},
}
app.Action = func(c *cli.Context) error { // 应用执行函数
// fmt.Printf("hello, %s \n", c.String("name"))
fmt.Printf("hello, %s \n", name)
return nil
}
_ = app.Run(os.Args) // 运行应用
}
Run:
go run main.go --name jack
#or
go run main.go --name=jack
#or
go run main.go -n jack
Output:
hello, jack
Command 使用示例
Code:
package main
import (
"fmt"
"github.com/urfave/cli"
"os"
)
func main() {
//var name string
app := cli.NewApp()
app.Name = "hello" // 应用名称
app.Usage = "say hello" // 应用功能说明
app.Author = "arrows" // 应用作者
app.Email = "shangmlee@foxmail.com" // 邮箱
app.Version = "0.0.1" // 版本
var name string
var level int
app.Commands = []cli.Command{
{
Name : "say",
Aliases: []string{"s"},
Usage : "say hello",
Flags : []cli.Flag{
cli.StringFlag{
Name : "name, n", // 命令名称
Usage : "input your name", // 命令说明
Value : "Unknown", // 默认值
Destination: &name, // 赋值
},
cli.IntFlag{
Name : "level, l", // 命令名称
Usage : "input your level", // 命令说明
Value : 0, // 默认值
Destination: &level, // 赋值
},
},
Action : func(c *cli.Context) error {
switch level {
case 0:
fmt.Printf("hello, %s \n", name)
case 1:
fmt.Printf("hello, %s ! You are super user \n", name)
default:
fmt.Printf("hello, %s ! no record \n", name)
}
return nil
},
},
}
_ = app.Run(os.Args) // 运行应用
}
Run:
go run main.go say -n tom -l 1
Output:
hello, tom ! You are super user
SubCommand 使用
Code:
package main
import (
"fmt"
"github.com/urfave/cli"
"os"
)
func main() {
//var name string
app := cli.NewApp()
app.Name = "hello" // 应用名称
app.Usage = "say hello" // 应用功能说明
app.Author = "arrows" // 应用作者
app.Email = "shangmlee@foxmail.com" // 邮箱
app.Version = "0.0.1" // 版本
app.Commands = []cli.Command{
{
Name : "mysql",
Aliases: []string{"d"},
Usage : "mysql operations ",
Subcommands: []cli.Command{
{
Name: "insert",
Usage: "insert data",
Action: func(c *cli.Context) error {
fmt.Println("insert subcommand")
return nil
},
},
{
Name: "delete",
Usage: "delete data",
Action: func(c *cli.Context) error {
fmt.Println("delete subcommand")
return nil
},
},
},
},
}
_ = app.Run(os.Args) // 运行应用
}
Run:
go run main.go mysql insert
Output:
insert subcommand
有疑问加站长微信联系(非本文作者)