Skip to content

Commit bb7601a

Browse files
committed
exit with status 1 if help is called on an invalid command.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
1 parent 1640bb2 commit bb7601a

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

cli/cobra.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/spf13/cobra"
78
)
@@ -17,6 +18,7 @@ func SetupRootCommand(rootCmd *cobra.Command) {
1718
rootCmd.SetUsageTemplate(usageTemplate)
1819
rootCmd.SetHelpTemplate(helpTemplate)
1920
rootCmd.SetFlagErrorFunc(FlagErrorFunc)
21+
rootCmd.SetHelpCommand(helpCommand)
2022

2123
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
2224
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
@@ -39,6 +41,23 @@ func FlagErrorFunc(cmd *cobra.Command, err error) error {
3941
}
4042
}
4143

44+
var helpCommand = &cobra.Command{
45+
Use: "help [command]",
46+
Short: "Help about the command",
47+
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
48+
PersistentPostRun: func(cmd *cobra.Command, args []string) {},
49+
RunE: func(c *cobra.Command, args []string) error {
50+
cmd, args, e := c.Root().Find(args)
51+
if cmd == nil || e != nil || len(args) > 0 {
52+
return fmt.Errorf("unknown help topic: %v", strings.Join(args, " "))
53+
}
54+
55+
helpFunc := cmd.HelpFunc()
56+
helpFunc(cmd, args)
57+
return nil
58+
},
59+
}
60+
4261
func hasSubCommands(cmd *cobra.Command) bool {
4362
return len(operationSubCommands(cmd)) > 0
4463
}

cmd/docker/docker_test.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package main
22

33
import (
4+
"io/ioutil"
45
"os"
56
"testing"
67

78
"github.com/Sirupsen/logrus"
8-
"github.com/docker/docker/utils"
9-
109
"github.com/docker/docker/cli/command"
10+
"github.com/docker/docker/pkg/testutil/assert"
11+
"github.com/docker/docker/utils"
1112
)
1213

1314
func TestClientDebugEnabled(t *testing.T) {
@@ -16,14 +17,16 @@ func TestClientDebugEnabled(t *testing.T) {
1617
cmd := newDockerCommand(&command.DockerCli{})
1718
cmd.Flags().Set("debug", "true")
1819

19-
if err := cmd.PersistentPreRunE(cmd, []string{}); err != nil {
20-
t.Fatalf("Unexpected error: %s", err.Error())
21-
}
20+
err := cmd.PersistentPreRunE(cmd, []string{})
21+
assert.NilError(t, err)
22+
assert.Equal(t, os.Getenv("DEBUG"), "1")
23+
assert.Equal(t, logrus.GetLevel(), logrus.DebugLevel)
24+
}
2225

23-
if os.Getenv("DEBUG") != "1" {
24-
t.Fatal("expected debug enabled, got false")
25-
}
26-
if logrus.GetLevel() != logrus.DebugLevel {
27-
t.Fatalf("expected logrus debug level, got %v", logrus.GetLevel())
28-
}
26+
func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) {
27+
discard := ioutil.Discard
28+
cmd := newDockerCommand(command.NewDockerCli(os.Stdin, discard, discard))
29+
cmd.SetArgs([]string{"help", "invalid"})
30+
err := cmd.Execute()
31+
assert.Error(t, err, "unknown help topic: invalid")
2932
}

0 commit comments

Comments
 (0)