Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making initCompleteCmd exported so completion commands can be documented #1464

Closed
gssbzn opened this issue Jul 26, 2021 · 4 comments · Fixed by #1467
Closed

Making initCompleteCmd exported so completion commands can be documented #1464

gssbzn opened this issue Jul 26, 2021 · 4 comments · Fixed by #1467
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users

Comments

@gssbzn
Copy link
Contributor

gssbzn commented Jul 26, 2021

Right now if you try to generate documentation the usual way

package main

import (
	"log"

	"github.com/spf13/cobra"
	"github.com/spf13/cobra/doc"
)

func main() {
	cmd := &cobra.Command{
		Use:   "test",
		Short: "my test program",
	}
	err := doc.GenReSTTree(cmd, "/tmp")
	if err != nil {
		log.Fatal(err)
	}
}

Will not generate docs for the auto generated completion command, an alternative to get it to work would be to alter the example to

package main

import (
	"log"

	"github.com/spf13/cobra"
	"github.com/spf13/cobra/doc"
)

func main() {
	cmd := &cobra.Command{
		Use:   "test",
		Short: "my test program",
	}

	_, _ = cmd.ExecuteC() // init completion command indirectly

	err := doc.GenReSTTree(cmd, "/tmp")
	if err != nil {
		log.Fatal(err)
	}
}

but this will cause to init the help command which the current behaviour (I believe) is ok on not documenting

Is there any reasons why initCompleteCmd is not exported and could we make it so so I can replace _, _ = cmd.ExecuteC() with a call to just that function when documenting commands? InitDefaultHelpCmd seems to be exported so we would be aligning with that behaviour

Happy to make a PR if this is ok to change

Edit: this condition

if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
will prevent help from being documented but I still there's value on exporting initCompleteCmd to make this more focused to what's needed

@gssbzn gssbzn changed the title Making initCompleteCmd export so completion commands can be documented Making initCompleteCmd exported so completion commands can be documented Jul 26, 2021
@marckhouzam
Copy link
Collaborator

Hi @gssbzn. I am not too familiar with the REST generation provided by Cobra. If I try calling doc. GenReSTTree() from a sub-command of my program, then the "completion" command is properly documented. But that is because the Execute() function is called in that usecase.

I realize the Cobra documentation gives examples of REST generation that don't use Execute(), however, is this a valid scenario for you, or are you just trying the examples?

That being said, if we need to support generating REST without calling Execute(), I think you are right:
1- we'll need to make public InitDefaultCompletionCmd() (not initCompleteCmd() which sets up the hidden __complete command).
2- add a call to this new public function here:

cmd.InitDefaultHelpFlag()

Finally, you should know that the "completion" command is not created if there are no subcommands, so even with the above changes, I don't believe the example will include the "completion" command; you'd have to create a subcommand first.

@gssbzn
Copy link
Contributor Author

gssbzn commented Aug 3, 2021

Hi @marckhouzam thanks for looking into this, so expanding to a more complete example, the one I gave was basically from the docs but we've been documenting our commands before, completion included as we used to do it manually but since the change to the dynamic ones introduced in the latest cobra and now those files are no longer generated.

Full example

cmd := &cobra.Command{
		Use:   "test",
		Short: "my test program",
	}
	subcmd := &cobra.Command{
		Use:   "sub-test",
		Short: "my test command",
		Run: func(cmd *cobra.Command, args []string) {
			log.Println("test")
		},
	}
	cmd.AddCommand(subcmd)

	if err := doc.GenReSTTree(cmd, "./docs/command"); err != nil {
		log.Fatal(err)
	}

it will only generate the test.rst and test_sub-test.rst files, changing to doc.GenReSTTree(subcmd, "./docs/command") only generates test_sub-test.rst

Explicitly calling _ = cmd.Execute() before doc.GenReSTTree(cmd, "./docs/command"); generates the full completion command docs

Screenshot 2021-08-03 at 12 12 38

I think I'll go ahead and make initDefaultCompletionCmd public and document this behaviour better

@github-actions
Copy link

github-actions bot commented Oct 3, 2021

This issue is being marked as stale due to a long period of inactivity

@johnSchnake johnSchnake added area/lib Methods and functions that exist in the cobra library and consumed by users and removed kind/stale labels Feb 18, 2022
@lowdewijk
Copy link

I'd also like initDefaultCompletionCmd to be exported. The problem I am having is that I have some small alterations I want to do, which are impossible when this function is not exported.

I have done the same to the help command, which was possible, because InitDefaultHelpCmd is exported.

For reference the alterations I want to do:

  • Start the short message with a lowercase instead of uppercase character (this is part of our style guide)
  • I have placed all cli related commands under acli sub-command and I would like to move completions to it. So I would like it to be mycli cli completions instead of mycli completions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants