Skip to content

Commit

Permalink
💥 update(cmd): param args will exclude named arguments on run cmd.Func
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 5, 2023
1 parent 607a29b commit 76bdb02
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion base.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (b *base) Match(names []string) *Command {
return c
}

// FindCommand command by path. eg. "top:sub" or "top sub"
// FindCommand command by path. eg: "top:sub" or "top sub"
func (b *base) FindCommand(path string) *Command {
return b.Match(splitPath2names(path))
}
Expand Down
12 changes: 7 additions & 5 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func (c *Command) parseOptions(args []string) (ss []string, err error) {
return
}

// remaining args
// remaining args, next use for parse arguments
return c.RawArgs(), nil
}

Expand All @@ -511,20 +511,22 @@ func (c *Command) prepare(_ []string) (status int, err error) {
// do execute the command
func (c *Command) doExecute(args []string) (err error) {
// collect and binding named argument
Debugf("cmd: %s - collect and binding named argument", c.Name)
Debugf("cmd: %s - collect and binding named arguments", c.Name)
if err := c.ParseArgs(args); err != nil {
c.Fire(events.OnCmdRunError, map[string]any{"err": err})
Logf(VerbCrazy, "binding command '%s' arguments err: <red>%s</>", c.Name, err.Error())
Logf(VerbError, "binding command '%s' arguments err: <red>%s</>", c.Name, err.Error())
return err
}

c.Fire(events.OnCmdRunBefore, map[string]any{"args": args})
fnArgs := c.ExtraArgs()
c.Fire(events.OnCmdRunBefore, map[string]any{"args": fnArgs})
Debugf("cmd: %s - run command func with extra-args %v", c.Name, fnArgs)

// do call command handler func
if c.Func == nil {
Logf(VerbWarn, "the command '%s' no handler func to running", c.Name)
} else {
err = c.Func(c, args)
err = c.Func(c, fnArgs)
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
// HelpCommand name
HelpCommand = "help"
// VerbEnvName for set gcli debug level
VerbEnvName = "GCLI_VERB"
VerbEnvName = "GCLI_VERBOSE"
)

// constants for error level (quiet 0 - 5 crazy)
Expand Down
9 changes: 9 additions & 0 deletions gflag/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type CliArgs struct {
hasArrayArg bool
// mark exists optional argument
hasOptionalArg bool
// remain extra args after parse named arguments
remainArgs []string
}

// SetName for CliArgs
Expand Down Expand Up @@ -90,6 +92,8 @@ func (ags *CliArgs) ParseArgs(args []string) (err error) {
if ags.validateNum && inNum > num {
return errorx.Rawf("entered too many arguments: %v", args[num:])
}

ags.remainArgs = args[num:]
return
}

Expand Down Expand Up @@ -245,6 +249,11 @@ func (ags *CliArgs) BuildArgsHelp() string {
return sb.String()
}

// ExtraArgs remain extra args after collect parse.
func (ags *CliArgs) ExtraArgs() []string {
return ags.remainArgs
}

/*************************************************************
* Cli Argument definition
*************************************************************/
Expand Down
20 changes: 10 additions & 10 deletions gflag/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func (ops *CliOpts) BoolOpt(ptr *bool, name, shorts string, defVal bool, desc st
}

// BoolOpt2 binding a bool option, and allow with CliOptFn for config option.
func (ops *CliOpts) BoolOpt2(p *bool, nameWithShorts, desc string, setFns ...CliOptFn) {
ops.boolOpt(p, NewOpt(nameWithShorts, desc, false, setFns...))
func (ops *CliOpts) BoolOpt2(p *bool, nameAndShorts, desc string, setFns ...CliOptFn) {
ops.boolOpt(p, NewOpt(nameAndShorts, desc, false, setFns...))
}

// binding option and shorts
Expand Down Expand Up @@ -160,8 +160,8 @@ func (ops *CliOpts) StrOpt(p *string, name, shorts string, defValAndDesc ...stri
}

// StrOpt2 binding a string option, and allow with CliOptFn for config option.
func (ops *CliOpts) StrOpt2(p *string, nameWithShorts, desc string, setFns ...CliOptFn) {
ops.strOpt(p, NewOpt(nameWithShorts, desc, "", setFns...))
func (ops *CliOpts) StrOpt2(p *string, nameAndShorts, desc string, setFns ...CliOptFn) {
ops.strOpt(p, NewOpt(nameAndShorts, desc, "", setFns...))
}

// binding option and shorts
Expand Down Expand Up @@ -197,8 +197,8 @@ func (ops *CliOpts) IntOpt(p *int, name, shorts string, defVal int, desc string)
}

// IntOpt2 binding an int option and with config func.
func (ops *CliOpts) IntOpt2(p *int, nameWithShorts, desc string, setFns ...CliOptFn) {
opt := newOpt(nameWithShorts, desc, 0, "")
func (ops *CliOpts) IntOpt2(p *int, nameAndShorts, desc string, setFns ...CliOptFn) {
opt := newOpt(nameAndShorts, desc, 0, "")

ops.intOpt(p, opt.WithOptFns(setFns...))
}
Expand Down Expand Up @@ -497,14 +497,14 @@ type CliOpt struct {
}

// NewOpt quick create an CliOpt instance
func NewOpt(nameWithShorts, desc string, defVal any, setFns ...CliOptFn) *CliOpt {
return newOpt(nameWithShorts, desc, defVal, "").WithOptFns(setFns...)
func NewOpt(nameAndShorts, desc string, defVal any, setFns ...CliOptFn) *CliOpt {
return newOpt(nameAndShorts, desc, defVal, "").WithOptFns(setFns...)
}

// newOpt quick create an CliOpt instance
func newOpt(nameWithShorts, desc string, defVal any, shortcut string) *CliOpt {
func newOpt(nameAndShorts, desc string, defVal any, shortcut string) *CliOpt {
return &CliOpt{
Name: nameWithShorts,
Name: nameAndShorts,
Desc: desc,
// other info
DefVal: defVal,
Expand Down

0 comments on commit 76bdb02

Please sign in to comment.