diff --git a/acmd.go b/acmd.go index 7f096bd..f80228e 100644 --- a/acmd.go +++ b/acmd.go @@ -108,6 +108,9 @@ type Config struct { // VerboseHelp if "./app help -v" is passed, default is false. VerboseHelp bool + // AllowNoCommands set to true will not panic on empty list of commands. + AllowNoCommands bool + _ struct{} // enforce explicit field names. } @@ -124,7 +127,7 @@ func HasHelpFlag(flags []string) bool { // RunnerOf creates a Runner. func RunnerOf(cmds []Command, cfg Config) *Runner { - if len(cmds) == 0 { + if len(cmds) == 0 && !cfg.AllowNoCommands { panic("acmd: cannot run without commands") } diff --git a/acmd_test.go b/acmd_test.go index 4268ab8..aa54b15 100644 --- a/acmd_test.go +++ b/acmd_test.go @@ -155,6 +155,12 @@ func TestRunnerPanicWithoutCommands(t *testing.T) { RunnerOf(nil, Config{}) } +func TestRunner_AllowNoCommands(t *testing.T) { + RunnerOf(nil, Config{ + AllowNoCommands: true, + }) +} + func TestRunnerJustExit(t *testing.T) { var exitDone bool doExitOld := func(_ int) { @@ -248,15 +254,21 @@ func TestRunnerInit(t *testing.T) { wantErrStr: `duplicate command "a"`, }, { - cmds: []Command{{Name: "aaa", ExecFunc: nopFunc}, {Name: "b", Alias: "aaa", ExecFunc: nopFunc}}, + cmds: []Command{{Name: "aaa", ExecFunc: nopFunc}, { + Name: "b", Alias: "aaa", ExecFunc: nopFunc, + }}, wantErrStr: `duplicate command alias "aaa"`, }, { - cmds: []Command{{Name: "aaa", Alias: "a", ExecFunc: nopFunc}, {Name: "bbb", Alias: "a", ExecFunc: nopFunc}}, + cmds: []Command{{Name: "aaa", Alias: "a", ExecFunc: nopFunc}, { + Name: "bbb", Alias: "a", ExecFunc: nopFunc, + }}, wantErrStr: `duplicate command alias "a"`, }, { - cmds: []Command{{Name: "a", ExecFunc: nopFunc}, {Name: "b", Alias: "a", ExecFunc: nopFunc}}, + cmds: []Command{{Name: "a", ExecFunc: nopFunc}, { + Name: "b", Alias: "a", ExecFunc: nopFunc, + }}, wantErrStr: `duplicate command alias "a"`, }, }