Merge pull request #46394 from alexandercampbell/write-help-messages-to-stdout

Automatic merge from submit-queue (batch tested with PRs 46394, 46650, 46436, 46673, 46212)

Write "kubectl options" help message to stdout, not stderr

Fix a very minor issue causing `kubectl` to write its help messages to `stderr` instead of `stdout`.

Try this:

`kubectl options | grep log`

It should print only the options related to logging, but right now it prints the entire help menu (since it's printing to stderr).

This patch brings us closer to unix convention and reduces user friction.

~~Another use case (if a user can't remember whether it's `-r` or `-R` for recursion):~~

~~`kubectl patch -h | grep recursive`~~

Update: this patch only affects `kubectl options`. The other commands are working as intended.

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-05-31 00:14:00 -07:00 committed by GitHub
commit 9c7b59778c
3 changed files with 12 additions and 4 deletions

View File

@ -67,7 +67,7 @@ func NewKubeFedCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer, defa
templates.ActsAsRootCommand(cmds, filters, groups...) templates.ActsAsRootCommand(cmds, filters, groups...)
cmds.AddCommand(kubectl.NewCmdVersion(f, out)) cmds.AddCommand(kubectl.NewCmdVersion(f, out))
cmds.AddCommand(kubectl.NewCmdOptions()) cmds.AddCommand(kubectl.NewCmdOptions(out))
return cmds return cmds
} }

View File

@ -378,7 +378,7 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob
cmds.AddCommand(NewCmdVersion(f, out)) cmds.AddCommand(NewCmdVersion(f, out))
cmds.AddCommand(NewCmdApiVersions(f, out)) cmds.AddCommand(NewCmdApiVersions(f, out))
cmds.AddCommand(deprecatedAlias("apiversions", NewCmdApiVersions(f, out))) cmds.AddCommand(deprecatedAlias("apiversions", NewCmdApiVersions(f, out)))
cmds.AddCommand(NewCmdOptions()) cmds.AddCommand(NewCmdOptions(out))
return cmds return cmds
} }

View File

@ -17,6 +17,8 @@ limitations under the License.
package cmd package cmd
import ( import (
"io"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates" "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/util/i18n" "k8s.io/kubernetes/pkg/util/i18n"
@ -30,7 +32,7 @@ var (
) )
// NewCmdOptions implements the options command // NewCmdOptions implements the options command
func NewCmdOptions() *cobra.Command { func NewCmdOptions(out io.Writer) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "options", Use: "options",
Short: i18n.T("Print the list of flags inherited by all commands"), Short: i18n.T("Print the list of flags inherited by all commands"),
@ -41,7 +43,13 @@ func NewCmdOptions() *cobra.Command {
}, },
} }
templates.UseOptionsTemplates(cmd) // The `options` command needs write its output to the `out` stream
// (typically stdout). Without calling SetOutput here, the Usage()
// function call will fall back to stderr.
//
// See https://github.com/kubernetes/kubernetes/pull/46394 for details.
cmd.SetOutput(out)
templates.UseOptionsTemplates(cmd)
return cmd return cmd
} }