Skip to content

Commit

Permalink
chore: update some logs for run command, fix some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 7, 2022
1 parent e7b66d2 commit 64b783c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (app *App) findCommandName() (name string) {
// it is exists command name.
if app.IsCommand(name) {
app.inputName = rawName
Debugf("the raw input command: '<cyan>%s</>'; now, name: '<green>%s</>', args: %v", rawName, name, app.args)
Debugf("the raw input command: '<cyan>%s</>'; real name: '<green>%s</>', args: %v", rawName, name, app.args)
return name
}

Expand Down
11 changes: 4 additions & 7 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,6 @@ func (c *Command) initialize() {

// init for cmd Flags
c.Flags.InitFlagSet(cName)
// c.Flags.FSet().Usage = func() { // call on exists "-h" "--help"
// Logf(VerbDebug, "render help on exists '-h|--help' or has unknown flag")
// c.ShowHelp()
// }

// format description
if len(c.Desc) > 0 {
Expand Down Expand Up @@ -412,6 +408,8 @@ func (c *Command) Run(args []string) (err error) {

// dispatch execute the command
func (c *Command) innerDispatch(args []string) (err error) {
Debugf("cmd: %s - begin parse options by args: %v", c.Name, args)

// parse command flags
args, err = c.parseOptions(args)
if err != nil {
Expand Down Expand Up @@ -474,8 +472,7 @@ func (c *Command) innerDispatch(args []string) (err error) {

// not set command func and has sub commands.
if c.Func == nil && len(c.commands) > 0 {
// color.Cyanln()
Logf(VerbWarn, "cmd: %s - c.Func is empty, but has subcommands, render cmd list", c.Name)
Logf(VerbWarn, "cmd: %s - c.Func is empty, but has subcommands, render help", c.Name)
c.ShowHelp()
return err
}
Expand Down Expand Up @@ -515,7 +512,7 @@ func (c *Command) parseOptions(args []string) (ss []string, err error) {

// parse options, don't contains command name.
if err = c.Parse(args); err != nil {
Logf(VerbCrazy, "'%s' - parse options err: <red>%s</>", c.Name, err.Error())
Logf(VerbCrazy, "cmd: %s - parse options, err: <red>%s</>", c.Name, err.Error())
return
}

Expand Down
5 changes: 2 additions & 3 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestCommand_NewErrf(t *testing.T) {
is.Equal("error message", err.Error())
is.Equal([]string{"hi"}, c.RawArgs())

is.Panics(func() {
is.NotPanics(func() {
c.MustRun(simpleArgs)
})
}
Expand Down Expand Up @@ -121,8 +121,7 @@ func TestNewCommand_Run(t *testing.T) {
g := gcli.NewApp()
g.AddCommand(c)
err = c.Run(simpleArgs)
is.Error(err)

is.NoError(err)
}

var bf = new(bytes.Buffer)
Expand Down
9 changes: 4 additions & 5 deletions gargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ func (a *Argument) goodArgument() string {
if a.ShowName == "" {
a.ShowName = name
}

if !a.HasValue() {
a.Value = structs.NewValue(nil)
}
return name
}

Expand Down Expand Up @@ -347,11 +351,6 @@ func (a *Argument) HasValue() bool {
return a.Value != nil
}

// IsEmpty argument is empty
func (a *Argument) IsEmpty() bool {
return a.Name == ""
}

// Index get argument index in the command
func (a *Argument) Index() int {
return a.index
Expand Down
17 changes: 10 additions & 7 deletions gargs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/gookit/gcli/v3"
assert2 "github.com/gookit/goutil/testutil/assert"
"github.com/stretchr/testify/assert"
)

Expand All @@ -18,28 +19,30 @@ func TestCommand_AddArg(t *testing.T) {

ret := c.ArgByIndex(0)
is.Equal(ret, arg)
ret = c.ArgByIndex(1)
is.True(ret.IsEmpty())

assert2.PanicsMsg(t, func() {
c.ArgByIndex(1)
}, "GCli: get not exists argument #1")

arg = c.AddArg("arg1", "arg1 desc")
is.Equal(1, arg.Index())

ret = c.Arg("arg1")
is.Equal(ret, arg)

ret = c.Arg("not-exist")
is.True(ret.IsEmpty())
is.False(ret.HasValue())
assert2.PanicsMsg(t, func() {
c.Arg("not-exist")
}, "GCli: get not exists argument 'not-exist'")

is.Len(c.Args(), 2)

is.PanicsWithValue("GCli: the command argument name cannot be empty", func() {
c.AddArg("", "desc")
})

is.PanicsWithValue("GCli: the command argument name ':)&dfd' is invalid, must match: ^[a-zA-Z][\\w-]*$", func() {
assert2.PanicsMsg(t, func() {
c.AddArg(":)&dfd", "desc")
})
}, "GCli: the argument name ':)&dfd' is invalid, must match: ^[a-zA-Z][\\w-]*$")

is.PanicsWithValue("GCli: the argument name 'arg1' already exists in command 'test'", func() {
c.AddArg("arg1", "desc")
Expand Down
7 changes: 4 additions & 3 deletions gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ func (fs *Flags) Run(args []string) {

// do parsing
if err := fs.Parse(waitArgs); err != nil {
if err == flag.ErrHelp {
return // ignore help error
}

color.Errorf("Parse error: %s\n", err.Error())
}
}
Expand Down Expand Up @@ -224,9 +228,6 @@ func (fs *Flags) Parse(args []string) (err error) {

// do parsing
if err = fs.fSet.Parse(args); err != nil {
if err == flag.ErrHelp {
return nil // ignore help error
}
return err
}

Expand Down
17 changes: 4 additions & 13 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package gcli

import (
"fmt"
"path"
"regexp"
"runtime"
"strings"

"github.com/gookit/color"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/stdutil"
"github.com/gookit/goutil/strutil"
)

Expand Down Expand Up @@ -40,18 +39,10 @@ func logf(level VerbLevel, format string, v ...interface{}) {
return
}

var fnName string
pc, fName, line, ok := runtime.Caller(2)
if !ok {
fnName, fName, line = "UNKNOWN", "???.go", 0
} else {
fName = path.Base(fName)
fnName = runtime.FuncForPC(pc).Name()
}
name := level2color[level].Render(level.Upper())
logAt := stdutil.GetCallerInfo(3)

name := level.Upper()
name = level2color[level].Render(name)
color.Printf("GCli: [%s] [%s(), %s:%d] %s\n", name, fnName, fName, line, fmt.Sprintf(format, v...))
color.Printf("GCli: [%s] [<gray>%s</>] %s \n", name, logAt, fmt.Sprintf(format, v...))
}

func defaultErrHandler(data ...interface{}) (stop bool) {
Expand Down

0 comments on commit 64b783c

Please sign in to comment.