From 8e600e10f7b929832684ac0e762358b0c3c5b0b8 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 25 Oct 2017 18:22:04 -0400 Subject: [PATCH] Fix UseLine Signed-off-by: Daniel Nephin --- cli/cobra.go | 13 ++++++++++++- docs/yaml/yaml.go | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cli/cobra.go b/cli/cobra.go index 114f1b7d66..ede5b3a11d 100644 --- a/cli/cobra.go +++ b/cli/cobra.go @@ -17,6 +17,7 @@ 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) @@ -97,9 +98,19 @@ 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 +// 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/docs/yaml/yaml.go b/docs/yaml/yaml.go index 437321a8f6..a92688892d 100644 --- a/docs/yaml/yaml.go +++ b/docs/yaml/yaml.go @@ -8,6 +8,7 @@ import ( "sort" "strings" + "github.com/docker/cli/cli" "github.com/spf13/cobra" "github.com/spf13/pflag" yaml "gopkg.in/yaml.v2" @@ -92,7 +93,7 @@ func GenYamlCustom(cmd *cobra.Command, w io.Writer) error { } if cmd.Runnable() { - cliDoc.Usage = cmd.UseLine() + cliDoc.Usage = cli.UseLine(cmd) } if len(cmd.Example) > 0 {