Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5471863163 |
No files matched your search
@@ -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 <bash|zsh|fish|powershell>": exactly one argument, one of
|
||||
// Shells; anything else is a usage error. The script is cobra's own
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user