mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Provide option to hide default 'completion' cmd (#1541)
Fixes #1507 Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
This commit is contained in:
parent
ee75a2b1ed
commit
6f84ef4875
2 changed files with 18 additions and 0 deletions
|
@ -93,6 +93,8 @@ type CompletionOptions struct {
|
||||||
// DisableDescriptions turns off all completion descriptions for shells
|
// DisableDescriptions turns off all completion descriptions for shells
|
||||||
// that support them
|
// that support them
|
||||||
DisableDescriptions bool
|
DisableDescriptions bool
|
||||||
|
// HiddenDefaultCmd makes the default 'completion' command hidden
|
||||||
|
HiddenDefaultCmd bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NoFileCompletions can be used to disable file completion for commands that should
|
// 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()),
|
`, c.Root().Name()),
|
||||||
Args: NoArgs,
|
Args: NoArgs,
|
||||||
ValidArgsFunction: NoFileCompletions,
|
ValidArgsFunction: NoFileCompletions,
|
||||||
|
Hidden: c.CompletionOptions.HiddenDefaultCmd,
|
||||||
}
|
}
|
||||||
c.AddCommand(completionCmd)
|
c.AddCommand(completionCmd)
|
||||||
|
|
||||||
|
|
|
@ -2398,6 +2398,21 @@ func TestDefaultCompletionCmd(t *testing.T) {
|
||||||
rootCmd.CompletionOptions.DisableDescriptions = false
|
rootCmd.CompletionOptions.DisableDescriptions = false
|
||||||
// Remove completion command for the next test
|
// Remove completion command for the next test
|
||||||
removeCompCmd(rootCmd)
|
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) {
|
func TestCompleteCompletion(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue