Golang CLI框架介绍

  • Post author:
  • Post category:golang


网址:

https://github.com/mitchellh/cli



功能

该框架是个人开发的命令行程序框架,作者还成立了公司(HashiCorp),其公司的产品也采用这个CLI框架。



解读

框架的思路是:把命令和执行方法以map的形式记录在内部,然后根据用户输入的命令,决定执行哪个方法。实际上记录的是命令字符串和CommandFactory,由CommandFactory创建Command然后执行。

框架默认支持version和help两个功能。



示例

下面我们以例子来说明:(为了方便说明,所有的代码都在一个文件里,实际上应该分开多个文件)

package main

import (
	"fmt"
	"os"
	"github.com/mitchellh/cli"
)

func main() {
	c := cli.NewCLI("app", "1.0.0")    //这里指定了APP名字和版本
	c.Args = os.Args[1:]
	c.Commands = map[string]cli.CommandFactory{
		"foo": fooCommandFactory,    //定义foo命令和工厂
		"bar": barCommandFactory,    //定义bar命令和工厂
	}

	exitStatus, err := c.Run()
	if err != nil {
		fmt.Println(err)
	}

	os.Exit(exitStatus)
}

//foo命令工厂
func fooCommandFactory() (cli.Command, error) {
	fmt.Println("I am foo command")
	return new(FooCommand), nil
}
//bar命令工厂
func barCommandFactory() (cli.Command, error) {
	fmt.Println("I am bar command")
	return new(BarCommand), nil
}
//foo命令实现
type FooCommand struct{}

// Help should return long-form help text that includes the command-line
// usage, a brief few sentences explaining the function of the command,
// and the complete list of flags the command accepts.
func (f *FooCommand) Help() string {
	return "help foo"
}

// Run should run the actual command with the given CLI instance and
// command-line arguments. It should return the exit status when it is
// finished.
//
// There are a handful of special exit codes this can return documented
// above that change behavior.
func (f *FooCommand) Run(args []string) int {
	fmt.Println("Foo Command is running")
	return 1
}

// Synopsis should return a one-line, short synopsis of the command.
// This should be less than 50 characters ideally.
func (f *FooCommand) Synopsis() string {
	return "foo command Synopsis"
}

//bar命令实现
type BarCommand struct{}

// Help should return long-form help text that includes the command-line
// usage, a brief few sentences explaining the function of the command,
// and the complete list of flags the command accepts.
func (b *BarCommand) Help() string {
	return "help bar"
}

// Run should run the actual command with the given CLI instance and
// command-line arguments. It should return the exit status when it is
// finished.
//
// There are a handful of special exit codes this can return documented
// above that change behavior.
func (b *BarCommand) Run(args []string) int {
	fmt.Println("bar Command is running")
	return 1
}

// Synopsis should return a one-line, short synopsis of the command.
// This should be less than 50 characters ideally.
func (b *BarCommand) Synopsis() string {
	return "bar command Synopsis"
}

运行go build编译,然后运行,就可以看到执行结果:

name@mypc MINGW64 /d/go-repo/test
$ go build cli1

name@mypc MINGW64 /d/go-repo/test
$ ll
total 3924
-rwxr-xr-x 1 name 197121 4017152 2月  21 09:47 cli1.exe*
drwxr-xr-x 1 name 197121       0 2月  18 14:19 src/

name@mypc MINGW64 /d/go-repo/test
$ ./cli1
I am bar command
I am foo command
I am bar command
I am foo command
Usage: app [--version] [--help] <command> [<args>]

Available commands are:
    bar    bar command Synopsis
    foo    foo command Synopsis


name@mypc MINGW64 /d/go-repo/test
$ ./cli1 foo
I am bar command
I am foo command
I am foo command
Foo Command is running




版权声明:本文为fanzch原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。