diff --git a/command.go b/command.go index 76e2d31..7c0de0b 100644 --- a/command.go +++ b/command.go @@ -953,7 +953,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) { // initialize help at the last point to allow for user overriding c.InitDefaultHelpCmd() // initialize completion at the last point to allow for user overriding - c.initDefaultCompletionCmd() + c.InitDefaultCompletionCmd() args := c.args diff --git a/completions.go b/completions.go index 60c614d..72bef34 100644 --- a/completions.go +++ b/completions.go @@ -598,12 +598,12 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p return flag, trimmedArgs, lastArg, nil } -// initDefaultCompletionCmd adds a default 'completion' command to c. +// InitDefaultCompletionCmd adds a default 'completion' command to c. // This function will do nothing if any of the following is true: // 1- the feature has been explicitly disabled by the program, // 2- c has no subcommands (to avoid creating one), // 3- c already has a 'completion' command provided by the program. -func (c *Command) initDefaultCompletionCmd() { +func (c *Command) InitDefaultCompletionCmd() { if c.CompletionOptions.DisableDefaultCmd || !c.HasSubCommands() { return } diff --git a/doc/man_docs.go b/doc/man_docs.go index cb54dca..d602dcc 100644 --- a/doc/man_docs.go +++ b/doc/man_docs.go @@ -234,6 +234,7 @@ func manPrintOptions(buf io.StringWriter, command *cobra.Command) { func genMan(cmd *cobra.Command, header *GenManHeader) []byte { cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpFlag() + cmd.InitDefaultCompletionCmd() // something like `rootcmd-subcmd1-subcmd2` dashCommandName := strings.Replace(cmd.CommandPath(), " ", "-", -1) diff --git a/doc/man_docs_test.go b/doc/man_docs_test.go index 291915e..2741281 100644 --- a/doc/man_docs_test.go +++ b/doc/man_docs_test.go @@ -195,7 +195,7 @@ func TestGenManSeeAlso(t *testing.T) { if err := assertNextLineEquals(scanner, ".PP"); err != nil { t.Fatalf("First line after SEE ALSO wasn't break-indent: %v", err) } - if err := assertNextLineEquals(scanner, `\fBroot-bbb(1)\fP, \fBroot-ccc(1)\fP`); err != nil { + if err := assertNextLineEquals(scanner, `\fBroot-bbb(1)\fP, \fBroot-ccc(1)\fP, \fBroot-completion(1)\fP`); err != nil { t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err) } } diff --git a/doc/md_docs.go b/doc/md_docs.go index 9df609e..e9772dd 100644 --- a/doc/md_docs.go +++ b/doc/md_docs.go @@ -53,6 +53,7 @@ func GenMarkdown(cmd *cobra.Command, w io.Writer) error { func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error { cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpFlag() + cmd.InitDefaultCompletionCmd() buf := new(bytes.Buffer) name := cmd.CommandPath() diff --git a/doc/rest_docs.go b/doc/rest_docs.go index 051d8dc..77f49ec 100644 --- a/doc/rest_docs.go +++ b/doc/rest_docs.go @@ -61,6 +61,7 @@ func GenReST(cmd *cobra.Command, w io.Writer) error { func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, string) string) error { cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpFlag() + cmd.InitDefaultCompletionCmd() buf := new(bytes.Buffer) name := cmd.CommandPath() diff --git a/doc/yaml_docs.go b/doc/yaml_docs.go index 96e6ad7..1b668ee 100644 --- a/doc/yaml_docs.go +++ b/doc/yaml_docs.go @@ -92,6 +92,7 @@ func GenYaml(cmd *cobra.Command, w io.Writer) error { func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error { cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpFlag() + cmd.InitDefaultCompletionCmd() yamlDoc := cmdDoc{} yamlDoc.Name = cmd.CommandPath()