cli

package
v0.0.0-...-012d1c6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

cli Command-line interface

command解析参数,格式化输出命令,可用于控制台参数解析,也可以用于服务器端admin模块, 这里要解决的问题是如何自动化的绑定数据并作简单的数据验证以及信息输出,以便于高效开发

在分布式集群环境下,admin的开发更加复杂一些,需要将所有不同节点上的Command汇总并根据不同的策略将Command发送到特定的节点上, 常见的策略有:广播或随机选择节点

cli.go文件定义了所有用到的接口

特性

  • 基于反射自动解析参数
  • 支持简单的参数验证,比如参数个数验证,类型转换验证,参数范围验证
  • 支持默认参数
  • TODO:格式化输出help信息

使用示例

package cli

import (
	"log"
	"testing"
)

func TestCmd(t *testing.T) {
	args, err := ParseCommandLine("test 1 -a=test")
	if err != nil {
		t.Fatal(err)
	}

	app := New()
	_ = app.Add(&testCmd{})
	result, _ := app.Exec(args, map[string]string{"project": "Apollo"})
	t.Log(result)
}

type testCmd struct {
	Project string `cli:"meta"`
	Arg0    int    `cli:"desc=参数0"`
	Arg1    string `cli:"flag=a|arg1,default=aa,desc=参数1"`
}

func (cmd *testCmd) Run(ctx Context) error {
	log.Print(cmd.Project, "\t", cmd.Arg0, "\t", cmd.Arg1)
	return ctx.Text("test result ok")
}

常用的cli库

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoCommand       = errors.New("no command")
	ErrNotFoundCommand = errors.New("not found command")
	ErrNotSupport      = errors.New("not support")
	ErrInvalidTag      = errors.New("invalid tag")
	ErrNameConflict    = errors.New("name conflict")
)

Functions

func ParseCommandLine

func ParseCommandLine(s string) ([]string, error)

解析命令行参数 from: https://github.com/mgutz/str.git

Types

type Action

type Action interface {
	Run(ctx Context) error
}

最终需要执行的命令

type Command

type Command interface {
	Name() string
	Full() string
	Group() string
	Meta() map[string]string
	Subs() []Command
	Add(sub Command)
	CanRun() bool
	Run(ctx Context) error
}

Command 以树形结构组织,只有叶节点可以可以执行

type Context

type Context interface {
	NArg() int
	Arg(index int) string
	NFlag() int
	Flag(key string) []string
	Get(key string) string
	// 返回结果
	Result() interface{}
	JSON(value interface{}) error
	XML(value interface{}) error
	Text(format string, values ...interface{}) error
	Any(value interface{}) error
}

type Engine

type Engine interface {
	List() []Command
	Tree() []Command
	Add(action Action, opts ...Option) error
	Exec(args []string, metas Map) (interface{}, error)
}

Engine 用于管理所有Command信息 List只返回所有可执行的Command Tree会以树状的形式组织Command,只有叶节点可以执行

func New

func New() Engine

New 创建Engine

type Map

type Map = map[string]string

type Option

type Option func(o *Options)

func Group

func Group(g string) Option

func Meta

func Meta(meta map[string]string) Option

func Name

func Name(n string) Option

type Options

type Options struct {
	Name  string            // command名字,可以是xx/xx/xxx形式
	Group string            // command分组,可用于help格式化输出
	Meta  map[string]string // 自定义数据用于扩展
}

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

unix命令行格式解析 1:-表示shortcut,可以多个合并,例如,-h 表示help, -czvx,表示-c -z -v -x,如果带参数,则只会设置给最后一个 2:--表示全称,例如--help 3:后边可以紧跟一个参数,可以使用=连载一起写,也可以空格分隔 4:可以重复,相同的则合并成1个处理

func (*Parser) Parse

func (p *Parser) Parse(args []string) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL