diff --git a/api/client/volume/cmd.go b/api/client/volume/cmd.go index 1a8f74f7e7..bc8f55ccca 100644 --- a/api/client/volume/cmd.go +++ b/api/client/volume/cmd.go @@ -14,13 +14,9 @@ func NewVolumeCommand(dockerCli *client.DockerCli) *cobra.Command { cmd := &cobra.Command{ Use: "volume", Short: "Manage Docker volumes", - // TODO: remove once cobra is patched to handle this - RunE: func(cmd *cobra.Command, args []string) error { - fmt.Fprintf(dockerCli.Err(), "\n%s", cmd.UsageString()) - if len(args) > 0 { - return cli.StatusError{StatusCode: 1} - } - return nil + Args: cli.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString()) }, } cmd.AddCommand( diff --git a/api/client/volume/create.go b/api/client/volume/create.go index 89b37796d6..a38ff556cd 100644 --- a/api/client/volume/create.go +++ b/api/client/volume/create.go @@ -28,11 +28,8 @@ func newCreateCommand(dockerCli *client.DockerCli) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Create a volume", + Args: cli.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - // TODO: remove once cobra is patched to handle this - if err := cli.AcceptsNoArgs(args, cmd); err != nil { - return err - } return runCreate(dockerCli, opts) }, } diff --git a/api/client/volume/inspect.go b/api/client/volume/inspect.go index a47ee8bfe2..cbdd96b3ab 100644 --- a/api/client/volume/inspect.go +++ b/api/client/volume/inspect.go @@ -20,10 +20,8 @@ func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command { cmd := &cobra.Command{ Use: "inspect [OPTIONS] VOLUME [VOLUME...]", Short: "Return low-level information on a volume", + Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if err := cli.MinRequiredArgs(args, 1, cmd); err != nil { - return err - } opts.names = args return runInspect(dockerCli, opts) }, diff --git a/api/client/volume/list.go b/api/client/volume/list.go index 3510063ff9..01ea5a6ce9 100644 --- a/api/client/volume/list.go +++ b/api/client/volume/list.go @@ -34,11 +34,8 @@ func newListCommand(dockerCli *client.DockerCli) *cobra.Command { Use: "ls", Aliases: []string{"list"}, Short: "List volumes", + Args: cli.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - // TODO: remove once cobra is patched to handle this - if err := cli.AcceptsNoArgs(args, cmd); err != nil { - return err - } return runList(dockerCli, opts) }, } diff --git a/api/client/volume/remove.go b/api/client/volume/remove.go index d49597cc9d..09bd17ff0c 100644 --- a/api/client/volume/remove.go +++ b/api/client/volume/remove.go @@ -15,10 +15,8 @@ func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command { Use: "rm VOLUME [VOLUME]...", Aliases: []string{"remove"}, Short: "Remove a volume", + Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if err := cli.MinRequiredArgs(args, 1, cmd); err != nil { - return err - } return runRemove(dockerCli, args) }, } @@ -26,10 +24,11 @@ func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command { func runRemove(dockerCli *client.DockerCli, volumes []string) error { client := dockerCli.Client() - var status = 0 + ctx := context.Background() + status := 0 for _, name := range volumes { - if err := client.VolumeRemove(context.Background(), name); err != nil { + if err := client.VolumeRemove(ctx, name); err != nil { fmt.Fprintf(dockerCli.Err(), "%s\n", err) status = 1 continue diff --git a/cli/required.go b/cli/required.go index db1a98bd52..94710374e6 100644 --- a/cli/required.go +++ b/cli/required.go @@ -2,30 +2,19 @@ package cli import ( "fmt" + "strings" "github.com/spf13/cobra" ) -// MinRequiredArgs checks if the minimum number of args exists, and returns an -// error if they do not. -func MinRequiredArgs(args []string, min int, cmd *cobra.Command) error { - if len(args) >= min { +// NoArgs validate args and returns an error if there are any args +func NoArgs(cmd *cobra.Command, args []string) error { + if len(args) == 0 { return nil } - return fmt.Errorf( - "\"%s\" requires at least %d argument(s).\n\nUsage: %s\n\n%s", - cmd.CommandPath(), - min, - cmd.UseLine(), - cmd.Short, - ) -} - -// AcceptsNoArgs returns an error message if there are args -func AcceptsNoArgs(args []string, cmd *cobra.Command) error { - if len(args) == 0 { - return nil + if cmd.HasSubCommands() { + return fmt.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n")) } return fmt.Errorf( @@ -35,3 +24,19 @@ func AcceptsNoArgs(args []string, cmd *cobra.Command) error { cmd.Short, ) } + +// RequiresMinArgs returns an error if there is not at least min args +func RequiresMinArgs(min int) cobra.PositionalArgs { + return func(cmd *cobra.Command, args []string) error { + if len(args) >= min { + return nil + } + return fmt.Errorf( + "\"%s\" requires at least %d argument(s).\n\nUsage: %s\n\n%s", + cmd.CommandPath(), + min, + cmd.UseLine(), + cmd.Short, + ) + } +} diff --git a/integration-cli/docker_cli_help_test.go b/integration-cli/docker_cli_help_test.go index 5e27a6e663..50eb052ca2 100644 --- a/integration-cli/docker_cli_help_test.go +++ b/integration-cli/docker_cli_help_test.go @@ -335,7 +335,7 @@ func testCommand(cmd string, newEnvs []string, scanForHome bool, home string) er return fmt.Errorf("Should not have full usage on %q\n", args) } if strings.HasSuffix(stderr, "\n\n") { - return fmt.Errorf("Should not have a blank line on %q\n", args) + return fmt.Errorf("Should not have a blank line on %q\n%v", args, stderr) } }