From 54718631632aca8cdd99019d8a012c40f1efa1aa Mon Sep 17 00:00:00 2001 From: Bjoern Kalkbrenner Date: Mon, 26 Oct 2026 14:48:52 +0100 Subject: [PATCH] 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 --- cobracmd/cobracmd.go | 30 ++++++++++++++++++++++++++++++ cobracmd/cobracmd_test.go | 7 +++++++ 2 files changed, 37 insertions(+) diff --git a/cobracmd/cobracmd.go b/cobracmd/cobracmd.go index dd0d35f..f59d42d 100644 --- a/cobracmd/cobracmd.go +++ b/cobracmd/cobracmd.go @@ -94,7 +94,13 @@ func IsUsage(err error) bool { // Execute runs root and returns the exit code: 0, 2 for a usage error, // 1 for any other error. cobra prints the error itself unless the // command sets SilenceErrors. +// +// Before running, it makes every command group below root strict (see +// StrictGroups): cobra itself rejects an unknown subcommand only at the +// root ("prog nosuch"), while "prog group nosuch" printed the group's +// help and exited 0. func Execute(root *cobra.Command) int { + StrictGroups(root) err := root.Execute() switch { case err == nil: @@ -106,6 +112,30 @@ func Execute(root *cobra.Command) int { } } +// StrictGroups makes every command below root that only groups +// subcommands (it has subcommands, no Run/RunE and no Args of its own) +// reject an unknown subcommand as a usage error; called without one it +// still prints its help (exit 0). Execute calls it; it is exported for +// programs that run root.Execute themselves. +func StrictGroups(root *cobra.Command) { + var walk func(c *cobra.Command) + walk = func(c *cobra.Command) { + for _, sub := range c.Commands() { + if sub.HasSubCommands() && !sub.Runnable() && sub.Args == nil { + sub.Args = func(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return Usage(fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())) + } + return nil + } + sub.RunE = func(cmd *cobra.Command, _ []string) error { return cmd.Help() } + } + walk(sub) + } + } + walk(root) +} + // Completion replaces cobra's default completion command on root with // "completion ": exactly one argument, one of // Shells; anything else is a usage error. The script is cobra's own diff --git a/cobracmd/cobracmd_test.go b/cobracmd/cobracmd_test.go index 46728a7..366183b 100644 --- a/cobracmd/cobracmd_test.go +++ b/cobracmd/cobracmd_test.go @@ -15,6 +15,9 @@ func newRoot() *cobra.Command { root.AddCommand(&cobra.Command{Use: "one ", 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)