Skip to content

Commit

Permalink
Provide option to hide default 'completion' cmd (spf13#1541)
Browse files Browse the repository at this point in the history
Fixes spf13#1507

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
  • Loading branch information
marckhouzam authored and umarcor committed Nov 25, 2021
1 parent 8c5d824 commit 90763a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type CompletionOptions struct {
// DisableDescriptions turns off all completion descriptions for shells
// that support them
DisableDescriptions bool
// HiddenDefaultCmd makes the default 'completion' command hidden
HiddenDefaultCmd bool
}

// NoFileCompletions can be used to disable file completion for commands that should
Expand Down Expand Up @@ -605,6 +607,7 @@ See each sub-command's help for details on how to use the generated script.
`, c.Root().Name()),
Args: NoArgs,
ValidArgsFunction: NoFileCompletions,
Hidden: c.CompletionOptions.HiddenDefaultCmd,
}
c.AddCommand(completionCmd)

Expand Down
15 changes: 15 additions & 0 deletions completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,21 @@ func TestDefaultCompletionCmd(t *testing.T) {
rootCmd.CompletionOptions.DisableDescriptions = false
// Remove completion command for the next test
removeCompCmd(rootCmd)

// Test that the 'completion' command can be hidden
rootCmd.CompletionOptions.HiddenDefaultCmd = true
assertNoErr(t, rootCmd.Execute())
compCmd, _, err = rootCmd.Find([]string{compCmdName})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if compCmd.Hidden == false {
t.Error("Default 'completion' command should be hidden but it is not")
}
// Re-enable for next test
rootCmd.CompletionOptions.HiddenDefaultCmd = false
// Remove completion command for the next test
removeCompCmd(rootCmd)
}

func TestCompleteCompletion(t *testing.T) {
Expand Down

0 comments on commit 90763a2

Please sign in to comment.