Signed-off-by: Daniel Nephin <dnephin@docker.com>
(cherry picked from commit bb7601a3ffdc78bbe7efe7e77a196e09cf3607c7)
Signed-off-by: Victor Vieux <victorvieux@gmail.com>
| ... | ... |
@@ -2,6 +2,7 @@ package cli |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"fmt" |
| 5 |
+ "strings" |
|
| 5 | 6 |
|
| 6 | 7 |
"github.com/spf13/cobra" |
| 7 | 8 |
) |
| ... | ... |
@@ -17,6 +18,7 @@ func SetupRootCommand(rootCmd *cobra.Command) {
|
| 17 | 17 |
rootCmd.SetUsageTemplate(usageTemplate) |
| 18 | 18 |
rootCmd.SetHelpTemplate(helpTemplate) |
| 19 | 19 |
rootCmd.SetFlagErrorFunc(FlagErrorFunc) |
| 20 |
+ rootCmd.SetHelpCommand(helpCommand) |
|
| 20 | 21 |
|
| 21 | 22 |
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
|
| 22 | 23 |
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
|
| ... | ... |
@@ -39,6 +41,23 @@ func FlagErrorFunc(cmd *cobra.Command, err error) error {
|
| 39 | 39 |
} |
| 40 | 40 |
} |
| 41 | 41 |
|
| 42 |
+var helpCommand = &cobra.Command{
|
|
| 43 |
+ Use: "help [command]", |
|
| 44 |
+ Short: "Help about the command", |
|
| 45 |
+ PersistentPreRun: func(cmd *cobra.Command, args []string) {},
|
|
| 46 |
+ PersistentPostRun: func(cmd *cobra.Command, args []string) {},
|
|
| 47 |
+ RunE: func(c *cobra.Command, args []string) error {
|
|
| 48 |
+ cmd, args, e := c.Root().Find(args) |
|
| 49 |
+ if cmd == nil || e != nil || len(args) > 0 {
|
|
| 50 |
+ return fmt.Errorf("unknown help topic: %v", strings.Join(args, " "))
|
|
| 51 |
+ } |
|
| 52 |
+ |
|
| 53 |
+ helpFunc := cmd.HelpFunc() |
|
| 54 |
+ helpFunc(cmd, args) |
|
| 55 |
+ return nil |
|
| 56 |
+ }, |
|
| 57 |
+} |
|
| 58 |
+ |
|
| 42 | 59 |
func hasSubCommands(cmd *cobra.Command) bool {
|
| 43 | 60 |
return len(operationSubCommands(cmd)) > 0 |
| 44 | 61 |
} |
| ... | ... |
@@ -1,13 +1,14 @@ |
| 1 | 1 |
package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "io/ioutil" |
|
| 4 | 5 |
"os" |
| 5 | 6 |
"testing" |
| 6 | 7 |
|
| 7 | 8 |
"github.com/Sirupsen/logrus" |
| 8 |
- "github.com/docker/docker/utils" |
|
| 9 |
- |
|
| 10 | 9 |
"github.com/docker/docker/cli/command" |
| 10 |
+ "github.com/docker/docker/pkg/testutil/assert" |
|
| 11 |
+ "github.com/docker/docker/utils" |
|
| 11 | 12 |
) |
| 12 | 13 |
|
| 13 | 14 |
func TestClientDebugEnabled(t *testing.T) {
|
| ... | ... |
@@ -16,14 +17,16 @@ func TestClientDebugEnabled(t *testing.T) {
|
| 16 | 16 |
cmd := newDockerCommand(&command.DockerCli{})
|
| 17 | 17 |
cmd.Flags().Set("debug", "true")
|
| 18 | 18 |
|
| 19 |
- if err := cmd.PersistentPreRunE(cmd, []string{}); err != nil {
|
|
| 20 |
- t.Fatalf("Unexpected error: %s", err.Error())
|
|
| 21 |
- } |
|
| 19 |
+ err := cmd.PersistentPreRunE(cmd, []string{})
|
|
| 20 |
+ assert.NilError(t, err) |
|
| 21 |
+ assert.Equal(t, os.Getenv("DEBUG"), "1")
|
|
| 22 |
+ assert.Equal(t, logrus.GetLevel(), logrus.DebugLevel) |
|
| 23 |
+} |
|
| 22 | 24 |
|
| 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 |
- } |
|
| 25 |
+func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) {
|
|
| 26 |
+ discard := ioutil.Discard |
|
| 27 |
+ cmd := newDockerCommand(command.NewDockerCli(os.Stdin, discard, discard)) |
|
| 28 |
+ cmd.SetArgs([]string{"help", "invalid"})
|
|
| 29 |
+ err := cmd.Execute() |
|
| 30 |
+ assert.Error(t, err, "unknown help topic: invalid") |
|
| 29 | 31 |
} |