Skip to content

Commit

Permalink
Use proper help format for "completion" (#613)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
marckhouzam authored Dec 15, 2023
1 parent 85ea46d commit e879460
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
20 changes: 12 additions & 8 deletions pkg/command/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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:
Expand Down Expand Up @@ -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) {
Expand All @@ -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")
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/command/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/framework/framework_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e879460

Please sign in to comment.