cobracmd: unknown subcommands of a command group are usage errors (exit 2)

cobra rejects an unknown subcommand only at the root; "prog group nosuch"
printed the group's help and exited 0. Execute now calls StrictGroups
first (found in yamase-ctl while rolling out cobracmd/v0.1.0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
terminarandClaude Opus 5.5 committed 2026-10-26 14:48:52 +01:00
1 parent 5288c4ce74
commit 5471863163
2 files changed
+37

No files matched your search

+7
View File
@@ -15,6 +15,9 @@ func newRoot() *cobra.Command {
root.AddCommand(&cobra.Command{Use: "one <arg>", Args: cobra.ExactArgs(1), RunE: func(*cobra.Command, []string) error { return nil }})
root.AddCommand(&cobra.Command{Use: "fail", RunE: func(*cobra.Command, []string) error { return errors.New("broken") }})
root.AddCommand(&cobra.Command{Use: "bad", RunE: func(*cobra.Command, []string) error { return Usage(errors.New("wrong")) }})
group := &cobra.Command{Use: "queue", Short: "queue commands"}
group.AddCommand(&cobra.Command{Use: "list", RunE: func(*cobra.Command, []string) error { return nil }})
root.AddCommand(group)
Completion(root)
return root
}
@@ -43,6 +46,10 @@ func TestExitCodes(t *testing.T) {
{[]string{"completion"}, ExitUsage},
{[]string{"completion", "tcsh"}, ExitUsage},
{[]string{"completion", "bash", "zsh"}, ExitUsage},
{[]string{"queue"}, ExitOK},
{[]string{"queue", "list"}, ExitOK},
{[]string{"queue", "nosuch"}, ExitUsage},
{[]string{"queue", "--nosuch"}, ExitUsage},
} {
if got, out := run(tc.args...); got != tc.want {
t.Errorf("%v: exit %d, want %d (%s)", tc.args, got, tc.want, out)