Skip to content

Commit

Permalink
up: update some flag logic and fix some tests error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 11, 2022
1 parent 133265b commit 08d19ee
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 188 deletions.
11 changes: 6 additions & 5 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gookit/color"
"github.com/gookit/gcli/v3"
"github.com/gookit/goutil/dump"
assert2 "github.com/gookit/goutil/testutil/assert"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -240,7 +241,7 @@ func TestApp_Run_command_withArguments(t *testing.T) {
}

func TestApp_Run_command_withOptions(t *testing.T) {
is := assert.New(t)
is := assert2.New(t)
app := gcli.NewApp(gcli.NotExitOnEnd())

// run with command
Expand All @@ -264,10 +265,10 @@ func TestApp_Run_command_withOptions(t *testing.T) {

// run command
code := app.Run([]string{"test"})
is.Equal(0, code)
is.Equal("", optStr)
is.Equal("test", cmdRet)
is.Equal("test", app.CommandName())
is.Eq(0, code)
is.Eq("", optStr)
is.Eq("test", cmdRet)
is.Eq("test", app.CommandName())

// help option
app.Run([]string{"test", "-h"})
Expand Down
15 changes: 3 additions & 12 deletions gargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,18 @@ func (a *Argument) goodArgument() string {
a.ShowName = name
}

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

// GetValue get value by custom handler func
func (a *Argument) GetValue() interface{} {
val := a.Value
val := a.Value.Val()
if a.Handler != nil {
return a.Handler(val)
}

return val
}

Expand All @@ -338,17 +337,9 @@ func (a *Argument) Array() (ss []string) {
return a.Strings()
}

// Strings argument value to string array, if argument isArray = true.
func (a *Argument) Strings() (ss []string) {
if a.Value != nil && a.Arrayed {
ss = a.Value.Strings()
}
return
}

// HasValue value is empty
func (a *Argument) HasValue() bool {
return a.Value != nil
return a.V != nil
}

// Index get argument index in the command
Expand Down
60 changes: 29 additions & 31 deletions gargs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,62 +76,60 @@ func TestArguments_BindArg(t *testing.T) {
}

func TestArgument(t *testing.T) {
is := assert.New(t)
is := assert2.New(t)
arg := gcli.NewArgument("arg0", "arg desc")

is.False(arg.Arrayed)
is.False(arg.Required)
is.False(arg.IsEmpty())
is.True(arg.IsEmpty())
is.False(arg.HasValue())

is.Equal("arg0", arg.Name)
is.Equal("arg desc", arg.Desc)
is.Equal(0, arg.Index())
is.Eq("arg0", arg.Name)
is.Eq("arg desc", arg.Desc)
is.Eq(0, arg.Index())

// no value
is.Nil(arg.Strings())
is.Nil(arg.GetValue())
is.Nil(arg.SplitToStrings())
is.Equal(0, arg.Int())
is.Equal("", arg.String())
is.Equal("ab", arg.WithValue("ab").String())
is.Eq(0, arg.Int())
is.Eq("", arg.String())
is.Eq("ab", arg.WithValue("ab").String())

// add value
err := arg.SetValue("ab,cd")
is.NoError(err)

is.Nil(arg.Strings())
is.Equal(0, arg.Int())
is.NoErr(err)

is.Equal("ab,cd", arg.String())
is.Equal([]string{"ab", "cd"}, arg.Strings())
is.Equal([]string{"ab", "cd"}, arg.SplitToStrings(","))
is.Eq(0, arg.Int())
is.Eq("ab,cd", arg.String())
is.Eq([]string{"ab", "cd"}, arg.Array())
is.Eq([]string{"ab", "cd"}, arg.SplitToStrings(","))

// int value
is.NoError(arg.SetValue(23))
is.Equal(23, arg.Int())
is.Equal("23", arg.String())
is.NoErr(arg.SetValue(23))
is.Eq(23, arg.Int())
is.Eq("23", arg.String())

// string int value
err = arg.SetValue("23")
is.NoError(err)
is.Equal(23, arg.Int())
is.Equal("23", arg.String())
is.NoErr(err)
is.Eq(23, arg.Int())
is.Eq("23", arg.String())

// array value
arg.Arrayed = true
is.NoError(arg.SetValue([]string{"a", "b"}))
is.NoErr(arg.SetValue([]string{"a", "b"}))
is.True(arg.Arrayed)
is.Equal(0, arg.Int())
is.Equal("", arg.String())
is.Equal([]string{"a", "b"}, arg.Array())
is.Eq(0, arg.Int())
is.Eq("[a b]", arg.String())
is.Eq([]string{"a", "b"}, arg.Array())

// required and is-array
arg = gcli.NewArgument("arg1", "arg desc", true, true)
arg.Init()
is.True(arg.Arrayed)
is.True(arg.Required)
is.Equal("arg1...", arg.HelpName())
is.Eq("arg1...", arg.HelpName())
}

func TestArgument_GetValue(t *testing.T) {
Expand All @@ -156,7 +154,7 @@ func TestArgument_WithConfig(t *testing.T) {
arg.Init()
})

assert.Equal(t, 23, arg.Value)
assert.Equal(t, 23, arg.Val())
assert.Equal(t, "arg0", arg.HelpName())
}

Expand All @@ -167,16 +165,16 @@ func TestArgument_SetValue(t *testing.T) {

err := arg.SetValue("12")
assert.NoError(t, err)
assert.Equal(t, 12, arg.Value)
arg.Value = nil // reset value
assert.Equal(t, 12, arg.Val())
arg.Set(nil) // reset value

err = arg.SetValue("abc")
assert.Error(t, err)
assert.Nil(t, arg.Value)
assert.Nil(t, arg.Val())

// convert "12" to 12
arg = gcli.NewArgument("arg0", "arg desc").WithValidator(str2int)
err = arg.SetValue("12")
assert.NoError(t, err)
assert.Equal(t, 12, arg.Value)
assert.Equal(t, 12, arg.Val())
}
82 changes: 43 additions & 39 deletions gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ func (fs *Flags) formatOneFlag(f *flag.Flag) {
}

// add prefix '-' to option
fullName = cflag.AddPrefixes(name, meta.Shorts)
fullName = cflag.AddPrefixes2(name, meta.Shorts, true)
s = fmt.Sprintf(" <info>%s</>", fullName)

// - build flag type info
Expand Down Expand Up @@ -931,10 +931,51 @@ func newFlagMeta(name, desc string, defVal any, shortcut string) *FlagMeta {
Desc: desc,
// other info
DefVal: defVal,
Shorts: cflag.SplitShortcut(shortcut),
Shorts: strings.Split(shortcut, ","),
}
}

func (m *FlagMeta) initCheck() string {
if m.Desc != "" {
desc := strings.Trim(m.Desc, "; ")
if strings.ContainsRune(desc, ';') {
// format: desc;required
parts := strutil.SplitNTrimmed(desc, ";", 2)
if ln := len(parts); ln > 1 {
bl, err := strutil.Bool(parts[1])
if err == nil && bl {
desc = parts[0]
m.Required = true
}
}
}

m.Desc = desc
}

// filter shorts
if len(m.Shorts) > 0 {
m.Shorts = cflag.FilterNames(m.Shorts)
}
return m.goodName()
}

// good name of the flag
func (m *FlagMeta) goodName() string {
name := strings.Trim(m.Name, "- ")
if name == "" {
panicf("option flag name cannot be empty")
}

if !goodName.MatchString(name) {
panicf("option flag name '%s' is invalid, must match: %s", name, regGoodName)
}

// update self name
m.Name = name
return name
}

// Shorts2String join shorts to a string
func (m *FlagMeta) Shorts2String(sep ...string) string {
if len(m.Shorts) == 0 {
Expand Down Expand Up @@ -972,40 +1013,3 @@ func (m *FlagMeta) DValue() *stdutil.Value {
}
return m.defVal
}

func (m *FlagMeta) initCheck() string {
if m.Desc != "" {
desc := strings.Trim(m.Desc, "; ")
if strings.ContainsRune(desc, ';') {
// format: desc;required
parts := strutil.SplitNTrimmed(desc, ";", 2)
if ln := len(parts); ln > 1 {
bl, err := strutil.Bool(parts[1])
if err == nil && bl {
desc = parts[0]
m.Required = true
}
}
}

m.Desc = desc
}

return m.goodName()
}

// good name of the flag
func (m *FlagMeta) goodName() string {
name := strings.Trim(m.Name, "- ")
if name == "" {
panicf("option flag name cannot be empty")
}

if !goodName.MatchString(name) {
panicf("option flag name '%s' is invalid, must match: %s", name, regGoodName)
}

// update self name
m.Name = name
return name
}
Loading

0 comments on commit 08d19ee

Please sign in to comment.