From 812c492524bac07d78033e3b7399e5538f1abbf4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 19 May 2018 02:46:27 +0200 Subject: [PATCH] Hide [flags] in usage output This patch hides the [flags] in the usage output of commands, using the new `.DisableFlagsInUseLine` option, instead of the temporary workaround added in 8e600e10f7b929832684ac0e762358b0c3c5b0b8 Before this change: docker run "docker run" requires at least 1 argument. See 'docker run --help'. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] [flags] Run a command in a new container After this change: docker run "docker run" requires at least 1 argument. See 'docker run --help'. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container Signed-off-by: Sebastiaan van Stijn --- cli/cobra.go | 13 +------------ cmd/docker/docker.go | 11 ++++++++++- docs/yaml/yaml.go | 3 +-- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/cli/cobra.go b/cli/cobra.go index 6c4a8c4bf643..1b5809e9e5ce 100644 --- a/cli/cobra.go +++ b/cli/cobra.go @@ -17,7 +17,6 @@ func SetupRootCommand(rootCmd *cobra.Command) { cobra.AddTemplateFunc("operationSubCommands", operationSubCommands) cobra.AddTemplateFunc("managementSubCommands", managementSubCommands) cobra.AddTemplateFunc("wrappedFlagUsages", wrappedFlagUsages) - cobra.AddTemplateFunc("useLine", UseLine) rootCmd.SetUsageTemplate(usageTemplate) rootCmd.SetHelpTemplate(helpTemplate) @@ -100,19 +99,9 @@ func managementSubCommands(cmd *cobra.Command) []*cobra.Command { return cmds } -// UseLine returns the usage line for a command. This implementation is different -// from the default Command.UseLine in that it does not add a `[flags]` to the -// end of the line. -func UseLine(cmd *cobra.Command) string { - if cmd.HasParent() { - return cmd.Parent().CommandPath() + " " + cmd.Use - } - return cmd.Use -} - var usageTemplate = `Usage: -{{- if not .HasSubCommands}} {{ useLine . }}{{end}} +{{- if not .HasSubCommands}} {{.UseLine}}{{end}} {{- if .HasSubCommands}} {{ .CommandPath}} COMMAND{{end}} {{ .Short | trim }} diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 24def2e00e71..68136657d907 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -41,7 +41,8 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command { } return isSupported(cmd, dockerCli) }, - Version: fmt.Sprintf("%s, build %s", cli.Version, cli.GitCommit), + Version: fmt.Sprintf("%s, build %s", cli.Version, cli.GitCommit), + DisableFlagsInUseLine: true, } cli.SetupRootCommand(cmd) @@ -57,11 +58,19 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command { cmd.SetOutput(dockerCli.Out()) commands.AddCommands(cmd, dockerCli) + disableFlagsInUseLine(cmd) setValidateArgs(dockerCli, cmd, flags, opts) return cmd } +func disableFlagsInUseLine(cmd *cobra.Command) { + visitAll(cmd, func(ccmd *cobra.Command) { + // do not add a `[flags]` to the end of the usage line. + ccmd.DisableFlagsInUseLine = true + }) +} + func setFlagErrorFunc(dockerCli *command.DockerCli, cmd *cobra.Command, flags *pflag.FlagSet, opts *cliflags.ClientOptions) { // When invoking `docker stack --nonsense`, we need to make sure FlagErrorFunc return appropriate // output if the feature is not supported. diff --git a/docs/yaml/yaml.go b/docs/yaml/yaml.go index 8512be339bbd..5dc71641928f 100644 --- a/docs/yaml/yaml.go +++ b/docs/yaml/yaml.go @@ -8,7 +8,6 @@ import ( "sort" "strings" - "github.com/docker/cli/cli" "github.com/spf13/cobra" "github.com/spf13/pflag" yaml "gopkg.in/yaml.v2" @@ -96,7 +95,7 @@ func GenYamlCustom(cmd *cobra.Command, w io.Writer) error { } if cmd.Runnable() { - cliDoc.Usage = cli.UseLine(cmd) + cliDoc.Usage = cmd.UseLine() } if len(cmd.Example) > 0 {