Skip to content
Myr

Partial Revert of #922 (#1068)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Issue Reference: https://github.com/spf13/cobra/issues/1056 https://github.com/spf13/cobra/pull/922 introduced a new error type that emitted when a command was not runnable. This caused all commands w/o a run function set to error w/ that message and a status code of 1. This change reverts the addition of that new error. Similar functionality can be accomplished by leveraging RunE.

authored by Joshua Harshman · committed by GitHub · parent 95f2f73 · Browse files · 2 files+3 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+3 added−14 removed

  • other:+3 added−14 removed

2 files+3 added−14 removed

modified:command_test.go+2 added-2 removed
901 unmodified lines
902 childCmd := &Command{Use: "child", Long: "Long description"}903 rootCmd.AddCommand(childCmd)904905 output, err := executeCommand(rootCmd, "child")906 if err != ErrSubCommandRequired {907 t.Errorf("Expected error")906 if err != nil {907 t.Errorf("Unexpected error: %v", err)908 }909910 checkStringContains(t, output, childCmd.Long)911}
1046 unmodified lines
modified:command.go+1 added-12 removed
16 unmodified lines
1718import (19 "bytes"20 "context"21 "errors"21 "fmt"22 "io"23 "os"24 "path/filepath"
2 unmodified lines
2728 flag "github.com/spf13/pflag"29)3032var ErrSubCommandRequired = errors.New("subcommand is required")3331// FParseErrWhitelist configures Flag parse errors to be ignored32type FParseErrWhitelist flag.ParseErrorsWhitelist3334// Command is just that, a command for your application.
762 unmodified lines
797 }798 }799800 if !c.Runnable() {804 return ErrSubCommandRequired801 return flag.ErrHelp802 }803804 c.preRun()805
142 unmodified lines
948 cmd.HelpFunc()(cmd, args)949 return cmd, nil950 }951955 // If command wasn't runnable, show full help, but do return the error.956 // This will result in apps by default returning a non-success exit code, but also gives them the option to957 // handle specially.958 if err == ErrSubCommandRequired {959 cmd.HelpFunc()(cmd, args)960 return cmd, err961 }962952 // If root command has SilentErrors flagged,953 // all subcommands should respect it954 if !cmd.SilenceErrors && !c.SilenceErrors {955 c.Println("Error:", err.Error())
672 unmodified lines