Skip to content

Commit 6f84ef4

Browse files
authored
Provide option to hide default 'completion' cmd (#1541)
Fixes #1507 Signed-off-by: Marc Khouzam <[email protected]>
1 parent ee75a2b commit 6f84ef4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

completions.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ type CompletionOptions struct {
9393
// DisableDescriptions turns off all completion descriptions for shells
9494
// that support them
9595
DisableDescriptions bool
96+
// HiddenDefaultCmd makes the default 'completion' command hidden
97+
HiddenDefaultCmd bool
9698
}
9799

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

completions_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,21 @@ func TestDefaultCompletionCmd(t *testing.T) {
23982398
rootCmd.CompletionOptions.DisableDescriptions = false
23992399
// Remove completion command for the next test
24002400
removeCompCmd(rootCmd)
2401+
2402+
// Test that the 'completion' command can be hidden
2403+
rootCmd.CompletionOptions.HiddenDefaultCmd = true
2404+
assertNoErr(t, rootCmd.Execute())
2405+
compCmd, _, err = rootCmd.Find([]string{compCmdName})
2406+
if err != nil {
2407+
t.Errorf("Unexpected error: %v", err)
2408+
}
2409+
if compCmd.Hidden == false {
2410+
t.Error("Default 'completion' command should be hidden but it is not")
2411+
}
2412+
// Re-enable for next test
2413+
rootCmd.CompletionOptions.HiddenDefaultCmd = false
2414+
// Remove completion command for the next test
2415+
removeCompCmd(rootCmd)
24012416
}
24022417

24032418
func TestCompleteCompletion(t *testing.T) {

0 commit comments

Comments
 (0)