Skip to content

Commit

Permalink
refactor: move all flag arguments manage logic to pkg /gflag
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 23, 2022
1 parent 0b253e2 commit 7322987
Show file tree
Hide file tree
Showing 9 changed files with 1,040 additions and 1,000 deletions.
10 changes: 5 additions & 5 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
Desc: "an simple command",
Func: func(c *gcli.Command, args []string) error {
dump.Println(c.Path(), args)
c.SetValue("simple", "simple command")
c.Set("simple", "simple command")
return nil
},
}
Expand All @@ -45,7 +45,7 @@ var (
Name: "sub1",
Desc: "desc for top1.sub1",
Func: func(c *gcli.Command, args []string) error {
c.SetValue("msg", c.App().GetVal("top1:sub1"))
c.Set("msg", c.App().Get("top1:sub1"))
return nil
},
},
Expand Down Expand Up @@ -291,12 +291,12 @@ func TestApp_Run_subcommand(t *testing.T) {
is := assert.New(t)
id := "top1:sub1"

appWithMl.SetValue(id, "TestApp_Run_subcommand")
appWithMl.Set(id, "TestApp_Run_subcommand")
appWithMl.Run([]string{"top1", "sub1"})

c := appWithMl.FindCommand(id)
is.NotEmpty(c)
is.Eq("TestApp_Run_subcommand", c.GetVal("msg"))
is.Eq("TestApp_Run_subcommand", c.Get("msg"))
}

func TestApp_Run_by_cmd_ID(t *testing.T) {
Expand Down Expand Up @@ -452,6 +452,6 @@ func (uc *UserCommand) Config(c *gcli.Command) {
c.StrOpt(&uc.opt1, "opt", "o", "", "desc")
}

func (uc *UserCommand) Execute(c *gcli.Command, args []string) error {
func (uc *UserCommand) Execute(_ *gcli.Command, _ []string) error {
return nil
}
10 changes: 10 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ func (ctx *Context) hasHelpKeywords() bool {
return strings.HasSuffix(ctx.argLine, " -h") || strings.HasSuffix(ctx.argLine, " --help")
}

// SetValue to ctx
func (ctx *Context) SetValue(key string, val any) {
ctx.Set(key, val)
}

// GetVal from ctx
func (ctx *Context) GetVal(key string) interface{} {
return ctx.Get(key)
}

/*************************************************************
* command Base
*************************************************************/
Expand Down
9 changes: 4 additions & 5 deletions cmd.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/events"
"github.com/gookit/gcli/v3/gflag"
"github.com/gookit/gcli/v3/helper"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/structs"
Expand Down Expand Up @@ -50,12 +51,10 @@ type Command struct {

// --- provide option and argument parse and binding.

// Flags options for the command
Flags
// Arguments for the command
Arguments
// Flags (options+arguments) for the command
gflag.Flags

// Name is the full command name.
// Name is the command name.
Name string
// Desc is the command description message.
Desc string
Expand Down
11 changes: 11 additions & 0 deletions gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ func NewFlags(nameWithDesc ...string) *gflag.Flags {
return gflag.New(nameWithDesc...)
}

// Argument alias of the gflag.Argument
type Argument = gflag.Argument

// Arguments alias of the gflag.Arguments
type Arguments = gflag.Arguments

// NewArgument quick create a new command argument
func NewArgument(name, desc string, requiredAndArrayed ...bool) *Argument {
return gflag.NewArg(name, desc, nil, requiredAndArrayed...)
}

/*************************************************************************
* global options
*************************************************************************/
Expand Down
23 changes: 11 additions & 12 deletions gargs.go → gflag/args.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package gcli
package gflag

import (
"strings"

"github.com/gookit/gcli/v3/gflag"
"github.com/gookit/gcli/v3/helper"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/structs"
Expand All @@ -20,10 +19,10 @@ type Arguments struct {
name string
// args definition for a command.
//
// eg. {
// eg. [
// {"arg0", "this is first argument", false, false},
// {"arg1", "this is second argument", false, false},
// }
// ]
args []*Argument
// record min length for args
// argsMinLen int
Expand Down Expand Up @@ -110,7 +109,7 @@ func (ags *Arguments) AddArg(name, desc string, requiredAndArrayed ...bool) *Arg

// AddArgByRule add an arg by simple string rule
func (ags *Arguments) AddArgByRule(name, rule string) *Argument {
mp := gflag.ParseSimpleRule(name, rule)
mp := ParseSimpleRule(name, rule)

required := strutil.QuietBool(mp["required"])
newArg := NewArgument(name, mp["desc"], required)
Expand Down Expand Up @@ -141,15 +140,15 @@ func (ags *Arguments) AddArgument(arg *Argument) *Argument {
// validate argument name
name := arg.goodArgument()
if _, has := ags.argsIndexes[name]; has {
panicf("the argument name '%s' already exists in command '%s'", name, ags.name)
helper.Panicf("the argument name '%s' already exists in command '%s'", name, ags.name)
}

if ags.hasArrayArg {
panicf("have defined an array argument, you cannot add argument '%s'", name)
helper.Panicf("have defined an array argument, you cannot add argument '%s'", name)
}

if arg.Required && ags.hasOptionalArg {
panicf("required argument '%s' cannot be defined after optional argument", name)
helper.Panicf("required argument '%s' cannot be defined after optional argument", name)
}

// add argument index record
Expand Down Expand Up @@ -200,15 +199,15 @@ func (ags *Arguments) HasArguments() bool {
func (ags *Arguments) Arg(name string) *Argument {
i, ok := ags.argsIndexes[name]
if !ok {
panicf("get not exists argument '%s'", name)
helper.Panicf("get not exists argument '%s'", name)
}
return ags.args[i]
}

// ArgByIndex get named arg by index
func (ags *Arguments) ArgByIndex(i int) *Argument {
if i >= len(ags.args) {
panicf("get not exists argument #%d", i)
helper.Panicf("get not exists argument #%d", i)
}
return ags.args[i]
}
Expand Down Expand Up @@ -308,11 +307,11 @@ func (a *Argument) Init() *Argument {
func (a *Argument) goodArgument() string {
name := strings.TrimSpace(a.Name)
if name == "" {
panicf("the command argument name cannot be empty")
helper.Panicf("the command argument name cannot be empty")
}

if !helper.IsGoodName(name) {
panicf("the argument name '%s' is invalid, must match: %s", name, helper.RegGoodName)
helper.Panicf("the argument name '%s' is invalid, must match: %s", name, helper.RegGoodName)
}

a.Name = name
Expand Down
7 changes: 4 additions & 3 deletions gargs_test.go → gflag/args_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package gcli_test
package gflag_test

import (
"strconv"
"strings"
"testing"

"github.com/gookit/gcli/v3"
"github.com/gookit/gcli/v3/gflag"
"github.com/gookit/goutil/testutil/assert"
)

Expand Down Expand Up @@ -124,7 +125,7 @@ func TestArgument(t *testing.T) {
is.Eq([]string{"a", "b"}, arg.Array())

// required and is-array
arg = gcli.NewArgument("arg1", "arg desc", true, true)
arg = gflag.NewArgument("arg1", "arg desc", true, true)
arg.Init()
is.True(arg.Arrayed)
is.True(arg.Required)
Expand All @@ -149,7 +150,7 @@ var str2int = func(val any) (any, error) {

func TestArgument_WithConfig(t *testing.T) {
arg := gcli.NewArgument("arg0", "arg desc").WithFn(func(arg *gcli.Argument) {
arg.SetValue(23)
_ = arg.SetValue(23)
arg.Init()
})

Expand Down
Loading

0 comments on commit 7322987

Please sign in to comment.