Skip to content

Commit

Permalink
feat: make InitDefaultCompletionCmd public
Browse files Browse the repository at this point in the history
I also took the liberty to directly call it when generating docs as
it seems that was already the case for `InitDefaultHelpCmd`

Merge spf13/cobra#1467

closes #1464
  • Loading branch information
gssbzn authored and hoshsadiq committed Feb 8, 2022
1 parent f800c15 commit d910441
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions doc/man_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion doc/man_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
1 change: 1 addition & 0 deletions doc/md_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions doc/rest_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions doc/yaml_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d910441

Please sign in to comment.