Skip to content

Commit 54b6f94

Browse files
committed
Remove NoExecError
1 parent 9e8d93e commit 54b6f94

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

command.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ import (
99
"github.com/mfridman/cli/pkg/suggest"
1010
)
1111

12-
// NoExecError is returned when a command has no execution function.
13-
type NoExecError struct {
14-
Command *Command
15-
}
16-
17-
func (e *NoExecError) Error() string {
18-
return fmt.Sprintf("command %q has no execution function", getCommandPath(e.Command.state.commandPath))
19-
}
20-
2112
// Command represents a CLI command or subcommand within the application's command hierarchy.
2213
type Command struct {
2314
// Name is always a single word representing the command's name. It is used to identify the

parse.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ func Parse(root *Command, args []string) error {
201201
root.state.Args = finalArgs
202202

203203
if current.Exec == nil {
204-
return &NoExecError{
205-
Command: root, // Pass the root command which has the state with the full path
206-
}
204+
return fmt.Errorf("command %q: no exec function defined", getCommandPath(root.state.commandPath))
207205
}
208206
return nil
209207
}

parse_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,20 @@ func TestParse(t *testing.T) {
9090
Name: "foo",
9191
Exec: func(ctx context.Context, s *State) error { return nil },
9292
SubCommands: []*Command{
93-
{Name: "bar"},
93+
{
94+
Name: "bar",
95+
Exec: func(ctx context.Context, s *State) error { return nil },
96+
SubCommands: []*Command{
97+
{
98+
Name: "baz",
99+
},
100+
},
101+
},
94102
},
95103
}
96-
err := Parse(cmd, []string{"bar"})
104+
err := Parse(cmd, []string{"bar", "baz"})
97105
require.Error(t, err)
98-
var noExecErr *NoExecError
99-
require.ErrorAs(t, err, &noExecErr)
100-
assert.ErrorContains(t, err, `command "foo bar" has no execution function`)
106+
assert.ErrorContains(t, err, `command "foo bar baz": no exec function defined`)
101107
})
102108
t.Run("parsing errors", func(t *testing.T) {
103109
t.Parallel()

0 commit comments

Comments
 (0)