OpenMix 出品:https://openmix.org
Mix XCLI
命令行交互开发框架
CLI Interactive Commander
Overview
一个命令行交互开发库,它能够让单个 CLI 程序可执行多个命令,同时它还包含命令行参数获取、全局 panic 捕捉与解决、程序后盾执行等命令行开发罕用性能。
Github
https://github.com/mix-go/xcli
Installation
go get github.com/mix-go/xcli
Quick start
package mainimport ( "github.com/mix-go/xcli" "github.com/mix-go/xcli/flag")func main() { xcli.SetName("app").SetVersion("0.0.0-alpha") cmd := &xcli.Command{ Name: "hello", Short: "Echo demo", Run: func() { name := flag.Match("n", "name").String("default") // do something }, } opt := &xcli.Option{ Names: []string{"n", "name"}, Usage: "Your name", } cmd.AddOption(opt) xcli.AddCommand(cmd).Run()}
编译后,查看整个命令行程序的帮忙
$ ./go_build_main_go Usage: ./go_build_main_go [OPTIONS] COMMAND [opt...]Commands: hello Echo demoGlobal Options: -h, --help Print usage -v, --version Print version informationRun './go_build_main_go COMMAND --help' for more information on a command.Developed with Mix Go framework. (openmix.org/mix-go)
查看命令行程序的版本信息
$ ./go_build_main_go -vapp 0.0.0-alpha, framework 1.0.9
查看 hello
命令的帮忙
$ ./go_build_main_go hello --helpUsage: ./go_build_main_go hello [opt...]Command Options: -n, --name Your nameDeveloped with Mix Go framework. (openmix.org/mix-go)
执行 hello
命令
$ ./go_build_main_go hello
Flag 参数获取
该 flag 比 golang 自带的更加好用,不须要 Parse 操作
参数规定 (局部UNIX格调+GNU格调)
/examples/app home -d -rf --debug -v vvv --page 23 -s=test --name=john arg0
命令:
- 第一个参数,能够为空:
home
- 第一个参数,能够为空:
选项:
- 短选项:一个中杠,如
-d
、-rf
- 长选项:二个中杠,如:
--debug
- 短选项:一个中杠,如
选项值:
- 无值:
-d
、-rf
、--debug
- 有值(空格):
-v vvv
、--page 23
- 有值(等号):
-s=test
、--name=john
- 无值:
参数:
- 没有定义
-
的参数:arg0
- 没有定义
获取选项,能够获取 String
、Bool
、Int64
、Float64
多种类型,也能够指定默认值。
name := flag.Match("n", "name").String("Xiao Ming")
获取第一个参数
arg0 := flag.Arguments().First().String()
获取全副参数
for k, v := range flag.Arguments().Values() { // do something}
Daemon 后盾执行
将命令行程序变为后盾执行,该办法只可在 Main 协程中应用。
process.Daemon()
咱们能够通过配合 flag
获取参数,实现通过某几个参数控制程序后盾执行。
if flag.Match("d", "daemon").Bool() { process.Daemon()}
下面就实现了一个当命令行参数中带有 -d/--daemon
参数时,程序就在后盾执行。
Handle panic 错误处理
h := func(next func()) { defer func() { if err := recover(); err != nil { // handle panic } }() next()}cmd := &xcli.Command{ Name: "hello", Short: "Echo demo", Run: func() { // do something },}xcli.Use(h).AddCommand(cmd).Run()
Application
咱们在编写代码时,可能会要用到 App 中的一些信息。
// 获取根底门路(二进制所在目录门路)xcli.App().BasePath// App名称xcli.App().Name// App版本号xcli.App().Version// 是否开启debugxcli.App().Debug
Singleton 单命令
当咱们的 CLI 只有一个命令时,只须要配置一下 Singleton
:
cmd := &xcli.Command{ Name: "hello", Short: "Echo demo", Run: func() { // do something }, Singleton: true,}
命令的 Options 将会在 -h/--help
中打印
$ ./go_build_main_go Usage: ./go_build_main_go [OPTIONS] COMMAND [opt...]Command Options: -n, --name Your nameGlobal Options: -h, --help Print usage -v, --version Print version informationRun './go_build_main_go --help' for more information on a command.Developed with Mix Go framework. (openmix.org/mix-go)
Default 默认执行
当咱们的 CLI 有 CUI 时,须要实现点击后默认启动 UI 界面,只须要配置一下 Default
:
cmd := &xcli.Command{ Name: "hello", Short: "Echo demo", Run: func() { // do something }, Default: true,}
License
Apache License Version 2.0, http://www.apache.org/licenses/