Mix XCLI V1.1 - Go 命令行交互开发框架

栏目: 软件资讯 · 发布时间: 3年前

内容简介:Mix XCLI 命令行交互开发框架 CLI Interactive Commander Overview 一个命令行交互开发库,它可以让单个 CLI 程序可执行多个命令,同时它还包括命令行参数获取、全局 panic 捕获与处理、程序后台执行等命令行开发...

Mix XCLI

命令行交互开发框架

CLI Interactive Commander

Overview

一个命令行交互开发库,它可以让单个 CLI 程序可执行多个命令,同时它还包括命令行参数获取、全局 panic 捕获与处理、程序后台执行等命令行开发常用功能。

https://github.com/mix-go/xcli

Installation

go get github.com/mix-go/xcli

Quick start

package main

import (
    "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 demo

Global Options:
  -h, --help    Print usage
  -v, --version Print version information

Run './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 -v
app 0.0.0-alpha, framework 1.0.9

查看 hello 命令的帮助

$ ./go_build_main_go hello --help
Usage: ./go_build_main_go hello [opt...]

Command Options:
  -n, --name    Your name

Developed 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

获取选项,可以获取 StringBoolInt64Float64 多种类型,也可以指定默认值。

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

// 是否开启debug
xcli.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 name

Global Options:
  -h, --help    Print usage
  -v, --version Print version information

Run './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/


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Foundations of PEAR

Foundations of PEAR

Good, Nathan A./ Kent, Allan / Springer-Verlag New York Inc / 2006-11 / $ 50.84

PEAR, the PHP Extension and Application Repository, is a bountiful resource for any PHP developer. Within its confines lie the tools that you need to do your job more quickly and efficiently. You need......一起来看看 《Foundations of PEAR》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具