Skip to content

Commit

Permalink
👔 up(cmd): update some cmd events fire logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 7, 2023
1 parent 1c0ad15 commit 3657720
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
7 changes: 0 additions & 7 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,13 +703,6 @@ func (c *Command) PathNames() []string {
return c.pathNames
}

// Errorf format message and add error to the command
//
// Deprecated: please use NewErrf()
func (c *Command) Errorf(format string, v ...any) error {
return fmt.Errorf(format, v...)
}

// NewErr format message and add error to the command
func (c *Command) NewErr(msg string) error { return errors.New(msg) }

Expand Down
10 changes: 6 additions & 4 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ const (

// OnCmdNotFound on top-command or subcommand not found.
//
// Data:
// {name: command-name}
// Ctx:
// {"name": name, "args": []string}
OnCmdNotFound = "cmd.not.found"

// OnAppCmdNotFound on top command not found
// OnAppCmdNotFound on top command not found.
// ctx: {"name": name, "args": []string}
OnAppCmdNotFound = "app.cmd.not.found"
// OnCmdSubNotFound on subcommand not found
// OnCmdSubNotFound on subcommand not found.
// ctx: {"name": name, "args": []string}
OnCmdSubNotFound = "cmd.sub.not.found"

// OnCmdOptParsed event
Expand Down
7 changes: 6 additions & 1 deletion ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,18 @@ func newHookCtx(name string, c *Command, data map[string]any) *HookCtx {
data = make(maputil.Data)
}

return &HookCtx{
hc := &HookCtx{
name: name,
Cmd: c,
Data: data,
// with empty context
Context: context.Background(),
}

if c != nil {
hc.App = c.app
}
return hc
}

// Err of event
Expand Down
6 changes: 6 additions & 0 deletions gflag/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ func (a *CliArg) SetArrayed() *CliArg {
return a
}

// WithDefault value to the argument
func (a *CliArg) WithDefault(val any) *CliArg {
a.Value.Set(val)
return a
}

// WithValue to the argument
func (a *CliArg) WithValue(val any) *CliArg {
a.Value.Set(val)
Expand Down
1 change: 1 addition & 0 deletions gflag/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestArgument(t *testing.T) {
is.Eq(0, arg.Int())
is.Eq("", arg.String())
is.Eq("ab", arg.WithValue("ab").String())
is.Eq("abc", arg.WithDefault("abc").String())

// add value
err := arg.SetValue("ab,cd")
Expand Down

0 comments on commit 3657720

Please sign in to comment.