diff --git a/acmd.go b/acmd.go index b77675b..4f12186 100644 --- a/acmd.go +++ b/acmd.go @@ -71,6 +71,10 @@ type Config struct { // RunnerOf creates a Runner. func RunnerOf(cmds []Command, cfg Config) *Runner { + if len(cmds) == 0 { + panic("acmd: cannot run without commands") + } + r := &Runner{ cmds: cmds, cfg: cfg, diff --git a/acmd_test.go b/acmd_test.go index cc4e4fb..7e2688c 100644 --- a/acmd_test.go +++ b/acmd_test.go @@ -64,6 +64,14 @@ func TestRunnerMustSortCommands(t *testing.T) { return r.cmds[i].Name < r.cmds[j].Name }) } +func TestRunnerPanicWithoutCommands(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Fatal("must be panic") + } + }() + RunnerOf(nil, Config{}) +} func TestRunnerInit(t *testing.T) { testCases := []struct { @@ -125,17 +133,17 @@ func TestRunner_suggestCommand(t *testing.T) { want: `"fooo" is not a subcommand, did you mean "foo"?` + "\n", }, { - cmds: []Command{}, + cmds: []Command{{Name: "for", Do: nopFunc}}, args: []string{"hell"}, want: `"hell" is not a subcommand, did you mean "help"?` + "\n", }, { - cmds: []Command{}, + cmds: []Command{{Name: "for", Do: nopFunc}}, args: []string{"verZION"}, want: "", }, { - cmds: []Command{}, + cmds: []Command{{Name: "for", Do: nopFunc}}, args: []string{"verZion"}, want: `"verZion" is not a subcommand, did you mean "version"?` + "\n", },