From e8794605c8a68f4c5f60a24eaabd3c800c4686db Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Fri, 15 Dec 2023 12:15:10 -0500 Subject: [PATCH] Use proper help format for "completion" (#613) The "completion" and "generate-all-docs" commands' help were using the root command's help format by mistake. This commit fixes that. Also, this commit improves the output of the list of shells in the help output of the "completion" command. Signed-off-by: Marc Khouzam --- pkg/command/completion.go | 20 ++++++++++++-------- pkg/command/doc.go | 2 ++ test/e2e/framework/framework_constants.go | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pkg/command/completion.go b/pkg/command/completion.go index 314f34d43..1c2ef78d6 100644 --- a/pkg/command/completion.go +++ b/pkg/command/completion.go @@ -13,9 +13,14 @@ import ( "github.com/lithammer/dedent" "github.com/spf13/cobra" + "github.com/vmware-tanzu/tanzu-cli/pkg/cli" "github.com/vmware-tanzu/tanzu-plugin-runtime/plugin" ) +func init() { + completionCmd.SetUsageFunc(cli.SubCmdUsageFunc) +} + const compNoMoreArgsMsg = "This command does not take any more arguments (but may accept flags)." var ( @@ -26,13 +31,12 @@ var ( "powershell", } - completionLongDesc = dedent.Dedent(` - Output shell completion code for the specified shell %v. + completionLongDesc = `Output shell completion code for the specified shell (%v). - The shell completion code must be evaluated to provide completion. See Examples - for how to perform this for your given shell. +The shell completion code must be evaluated to provide completion. See Examples +for how to perform this for your given shell. - Note for bash users: make sure the bash-completions package has been installed.`) +Note for bash users: make sure the bash-completions package has been installed.` completionExamples = dedent.Dedent(` # Bash instructions: @@ -91,9 +95,9 @@ var ( // completionCmd represents the completion command var completionCmd = &cobra.Command{ - Use: fmt.Sprintf("completion %v", completionShells), + Use: fmt.Sprintf("completion [%v]", strings.Join(completionShells, "|")), Short: "Output shell completion code", - Long: fmt.Sprintf(completionLongDesc, completionShells), + Long: fmt.Sprintf(completionLongDesc, strings.Join(completionShells, ", ")), Example: completionExamples, DisableFlagsInUseLine: true, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { @@ -112,7 +116,7 @@ var completionCmd = &cobra.Command{ func runCompletion(out io.Writer, cmd *cobra.Command, args []string) error { if length := len(args); length == 0 { - return fmt.Errorf("shell not specified, choose one of: %v", completionShells) + return fmt.Errorf("shell not specified, choose one of: %v", strings.Join(completionShells, ", ")) } else if length > 1 { return errors.New("too many arguments, expected only the shell type") } diff --git a/pkg/command/doc.go b/pkg/command/doc.go index db2fe67f4..b87f3bdef 100644 --- a/pkg/command/doc.go +++ b/pkg/command/doc.go @@ -67,6 +67,8 @@ func newGenAllDocsCmd() *cobra.Command { // Shell completion for this flag is file completion which is enabled by default genAllDocsCmd.Flags().StringVarP(&docsDir, "docs-dir", "d", DefaultDocsDir, "destination for docs output") + genAllDocsCmd.SetUsageFunc(cli.SubCmdUsageFunc) + return genAllDocsCmd } diff --git a/test/e2e/framework/framework_constants.go b/test/e2e/framework/framework_constants.go index d84da4ec8..d9a36e167 100644 --- a/test/e2e/framework/framework_constants.go +++ b/test/e2e/framework/framework_constants.go @@ -157,7 +157,7 @@ const ( PluginDescShouldExist = "there should be one plugin description" PluginNameShouldMatch = "plugin name should be same as input value" - CompletionWithoutShell = "shell not specified, choose one of: [bash zsh fish powershell]" + CompletionWithoutShell = "shell not specified, choose one of: bash, zsh, fish, powershell" CompletionOutputForBash = "bash completion V2 for tanzu" CompletionOutputForZsh = "zsh completion for tanzu" CompletionOutputForFish = "fish completion for tanzu"