From 6f84ef48750fe6355a267117837ba6ee3da254e7 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Tue, 7 Dec 2021 18:02:02 -0500 Subject: [PATCH] Provide option to hide default 'completion' cmd (#1541) Fixes #1507 Signed-off-by: Marc Khouzam --- completions.go | 3 +++ completions_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/completions.go b/completions.go index 8480f1cd9..ac25a9d03 100644 --- a/completions.go +++ b/completions.go @@ -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 @@ -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) diff --git a/completions_test.go b/completions_test.go index 48f173012..2d6c11dc3 100644 --- a/completions_test.go +++ b/completions_test.go @@ -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) {