Skip to content

Commit

Permalink
Allow no commands for RunnerOf
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Apr 25, 2024
1 parent 8def4f4 commit 52d285a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 4 additions & 1 deletion acmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand All @@ -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")
}

Expand Down
18 changes: 15 additions & 3 deletions acmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"`,
},
}
Expand Down

0 comments on commit 52d285a

Please sign in to comment.