Skip to content

Commit

Permalink
upsome for cmd match
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 22, 2021
1 parent 069efab commit 4ee716c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/gookit/gcli)](https://github.com/gookit/gcli)
[![Build Status](https://travis-ci.org/gookit/gcli.svg?branch=master)](https://travis-ci.org/gookit/gcli)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/60c587f0491748fcabc1b3fe08d98074)](https://app.codacy.com/app/inhere/gcli?utm_source=github.com&utm_medium=referral&utm_content=gookit/gcli&utm_campaign=Badge_Grade_Dashboard)
[![GoDoc](https://pkg.go.dev/github.com/gookit/gcli?status.svg)](https://godoc.org/github.com/gookit/gcli/v3)
[![GoDoc](https://godoc.org/github.com/gookit/gcli?status.svg)](https://pkg.go.dev/github.com/gookit/gcli/v3)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/gcli)](https://goreportcard.com/report/github.com/gookit/gcli)
[![Coverage Status](https://coveralls.io/repos/github/gookit/gcli/badge.svg?branch=master)](https://coveralls.io/github/gookit/gcli?branch=master)

A simple to use command line application, written using golang.

> Since `v2.3.0`, option binding and argument binding have been reconstructed, which may be incompatible with the previous ones.
A simple-to-use command line application, written using golang.

## [中文说明](README.zh-CN.md)

Expand Down
4 changes: 1 addition & 3 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/gookit/gcli)](https://github.com/gookit/gcli)
[![Build Status](https://travis-ci.org/gookit/gcli.svg?branch=master)](https://travis-ci.org/gookit/gcli)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/60c587f0491748fcabc1b3fe08d98074)](https://app.codacy.com/app/inhere/gcli?utm_source=github.com&utm_medium=referral&utm_content=gookit/gcli&utm_campaign=Badge_Grade_Dashboard)
[![GoDoc](https://pkg.go.dev/github.com/gookit/gcli?status.svg)](https://pkg.go.dev/github.com/gookit/gcli/v3)
[![GoDoc](https://godoc.org/github.com/gookit/gcli?status.svg)](https://pkg.go.dev/github.com/gookit/gcli/v3)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/gcli)](https://goreportcard.com/report/github.com/gookit/gcli)
[![Coverage Status](https://coveralls.io/repos/github/gookit/gcli/badge.svg?branch=master)](https://coveralls.io/github/gookit/gcli?branch=master)

一个Golang下的简单易用的命令行应用,工具库。包含运行命令,颜色风格,数据展示,进度显示,交互方法等

> `v2.3.0` 重构了选项绑定和参数绑定,可能会跟之前有一些不兼容。
## [ENGLISH](README.md)

The english introduction please ses **[README](README.md)**
Expand Down
22 changes: 19 additions & 3 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func NewCommand(name, desc string, fn ...func(c *Command)) *Command {

// SetFunc Settings command handler func
func (c *Command) Match(nodes []string) *Command {
// must ensure is initialized
c.initialize()

ln := len(nodes)
if ln == 0 {
return c
Expand Down Expand Up @@ -178,6 +181,18 @@ func (c *Command) AddCommand(sub *Command) {
c.commandBase.addCommand(sub)
}

// init core
func (c *Command) initCore(cmdName string) {
c.core.cmdLine = CLI

c.AddVars(c.innerHelpVars())
c.AddVars(map[string]string{
"cmd": cmdName,
// full command
"fullCmd": c.binFile + " " + cmdName,
})
}

// initialize works for the command
func (c *Command) initialize() {
if c.initialized {
Expand All @@ -188,7 +203,9 @@ func (c *Command) initialize() {
cName := c.goodName()

// init core
c.core.init(cName)
c.initCore(cName)
// c.core.init(cName)
// c.core = newCore(cName)

// init commandBase
c.commandBase = newCommandBase()
Expand Down Expand Up @@ -433,14 +450,13 @@ func (c *Command) Run(inArgs []string) (err error) {

// binding global options
bindingCommonGOpts(c.gFlags)

// TODO parse global options

// init the command
c.initialize()

// add default error handler.
c.Hooks.AddOn(EvtCmdError, defaultErrHandler)
c.AddOn(EvtCmdError, defaultErrHandler)

// check input args
if len(inArgs) == 0 {
Expand Down
3 changes: 2 additions & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,13 @@ var r = &gcli.Command{
}

func TestCommand_Match(t *testing.T) {
err := r.Run([]string{})
err := r.Run([]string{"git"})
fmt.Println(err)
c := r.MatchByPath("git:add")

assert.NotNil(t, c)
assert.Equal(t, "add", c.Name)
assert.Equal(t, "git", c.Parent().Name)
}

func TestCommand_RunWithSubs(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ type core struct {
GOptsBinder func(gf *Flags)
}

func newCore(cmdName string) core {
c := core{}

c.cmdLine = CLI
c.AddVars(c.innerHelpVars())
c.AddVars(map[string]string{
"cmd": cmdName,
// full command
"fullCmd": c.binFile + " " + cmdName,
})

return c
}

// init core
func (c core) init(cmdName string) {
c.cmdLine = CLI
Expand Down

0 comments on commit 4ee716c

Please sign in to comment.