diff --git a/README.md b/README.md index ef61f22..7e446e1 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/README.zh-CN.md b/README.zh-CN.md index 47f6fc3..ea52c9c 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -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)** diff --git a/cmd.go b/cmd.go index 4042c02..c5d2671 100644 --- a/cmd.go +++ b/cmd.go @@ -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 @@ -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 { @@ -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() @@ -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 { diff --git a/cmd_test.go b/cmd_test.go index bbf7efa..56280ba 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -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) { diff --git a/core.go b/core.go index 8430d2e..2ce682a 100644 --- a/core.go +++ b/core.go @@ -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