Skip to content

Commit

Permalink
Remove NoExecError
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Jan 7, 2025
1 parent 9e8d93e commit 54b6f94
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
9 changes: 0 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ import (
"github.com/mfridman/cli/pkg/suggest"
)

// NoExecError is returned when a command has no execution function.
type NoExecError struct {
Command *Command
}

func (e *NoExecError) Error() string {
return fmt.Sprintf("command %q has no execution function", getCommandPath(e.Command.state.commandPath))
}

// Command represents a CLI command or subcommand within the application's command hierarchy.
type Command struct {
// Name is always a single word representing the command's name. It is used to identify the
Expand Down
4 changes: 1 addition & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ func Parse(root *Command, args []string) error {
root.state.Args = finalArgs

if current.Exec == nil {
return &NoExecError{
Command: root, // Pass the root command which has the state with the full path
}
return fmt.Errorf("command %q: no exec function defined", getCommandPath(root.state.commandPath))
}
return nil
}
Expand Down
16 changes: 11 additions & 5 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,20 @@ func TestParse(t *testing.T) {
Name: "foo",
Exec: func(ctx context.Context, s *State) error { return nil },
SubCommands: []*Command{
{Name: "bar"},
{
Name: "bar",
Exec: func(ctx context.Context, s *State) error { return nil },
SubCommands: []*Command{
{
Name: "baz",
},
},
},
},
}
err := Parse(cmd, []string{"bar"})
err := Parse(cmd, []string{"bar", "baz"})
require.Error(t, err)
var noExecErr *NoExecError
require.ErrorAs(t, err, &noExecErr)
assert.ErrorContains(t, err, `command "foo bar" has no execution function`)
assert.ErrorContains(t, err, `command "foo bar baz": no exec function defined`)
})
t.Run("parsing errors", func(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 54b6f94

Please sign in to comment.