From 53a147170c087619bf80be0c05f7ebf30ac83bfe Mon Sep 17 00:00:00 2001
From: Gustavo Bazan <gssbzn@gmail.com>
Date: Tue, 3 Aug 2021 12:37:19 +0100
Subject: [PATCH 1/4] feat: make InitDefaultCompletionCmd public

---
 command.go           | 2 +-
 completions.go       | 4 ++--
 doc/man_docs.go      | 1 +
 doc/man_docs_test.go | 2 +-
 doc/md_docs.go       | 1 +
 doc/rest_docs.go     | 1 +
 doc/yaml_docs.go     | 1 +
 7 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/command.go b/command.go
index 2cc18891d..b60254253 100644
--- a/command.go
+++ b/command.go
@@ -930,7 +930,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 b849b9c84..746d7c420 100644
--- a/completions.go
+++ b/completions.go
@@ -568,12 +568,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 916e36144..0eeaf4cf6 100644
--- a/doc/man_docs.go
+++ b/doc/man_docs.go
@@ -201,6 +201,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 aa3f5f2a1..5e8ee4711 100644
--- a/doc/man_docs_test.go
+++ b/doc/man_docs_test.go
@@ -131,7 +131,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 5181af8dc..6f89fe287 100644
--- a/doc/md_docs.go
+++ b/doc/md_docs.go
@@ -54,6 +54,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 051d8dc83..77f49ec7a 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 96e6ad721..1b668eeda 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()

From 17ef38bdd81631e1d4d97ef33733d7f5fb56f43e Mon Sep 17 00:00:00 2001
From: Gustavo Bazan <gus.bazan@hey.com>
Date: Wed, 4 May 2022 16:23:46 +0100
Subject: [PATCH 2/4] PR comments

---
 doc/man_docs.go  | 1 -
 doc/md_docs.go   | 1 -
 doc/rest_docs.go | 1 -
 doc/yaml_docs.go | 1 -
 4 files changed, 4 deletions(-)

diff --git a/doc/man_docs.go b/doc/man_docs.go
index 0eeaf4cf6..916e36144 100644
--- a/doc/man_docs.go
+++ b/doc/man_docs.go
@@ -201,7 +201,6 @@ 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/md_docs.go b/doc/md_docs.go
index 6f89fe287..5181af8dc 100644
--- a/doc/md_docs.go
+++ b/doc/md_docs.go
@@ -54,7 +54,6 @@ 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 77f49ec7a..051d8dc83 100644
--- a/doc/rest_docs.go
+++ b/doc/rest_docs.go
@@ -61,7 +61,6 @@ 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 1b668eeda..96e6ad721 100644
--- a/doc/yaml_docs.go
+++ b/doc/yaml_docs.go
@@ -92,7 +92,6 @@ 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()

From c868d9060b9af7f8b29be4ab9fe8cbeb814d5201 Mon Sep 17 00:00:00 2001
From: Gustavo Bazan <gus.bazan@hey.com>
Date: Wed, 4 May 2022 16:24:33 +0100
Subject: [PATCH 3/4] Update man_docs_test.go

---
 doc/man_docs_test.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/doc/man_docs_test.go b/doc/man_docs_test.go
index c4258026e..f3fe3d613 100644
--- a/doc/man_docs_test.go
+++ b/doc/man_docs_test.go
@@ -131,7 +131,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, \fBroot-completion(1)\fP`); err != nil {
+	if err := assertNextLineEquals(scanner, `\fBroot-bbb(1)\fP, \fBroot-ccc(1)\fP`); err != nil {
 		t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err)
 	}
 }

From 92121768010b76e8230614ae05640103155cbd24 Mon Sep 17 00:00:00 2001
From: Gustavo Bazan <gus.bazan@hey.com>
Date: Wed, 5 Oct 2022 16:05:17 +0100
Subject: [PATCH 4/4] Update README.md

---
 doc/README.md | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/doc/README.md b/doc/README.md
index 6ea4eb662..8e07baae3 100644
--- a/doc/README.md
+++ b/doc/README.md
@@ -7,6 +7,11 @@
 
 ## Options
 ### `DisableAutoGenTag`
+
 You may set `cmd.DisableAutoGenTag = true`
 to _entirely_ remove the auto generated string "Auto generated by spf13/cobra..."
 from any documentation source.
+
+### `InitDefaultCompletionCmd`
+
+You may call `cmd.InitDefaultCompletionCmd()` to document the default autocompletion command.