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

Make the default completion command accessible #1523

Closed

Conversation

marckhouzam
Copy link
Collaborator

@marckhouzam marckhouzam commented Nov 3, 2021

Fixes #1507 as requested by @jpmcb

The ask of #1507 was to be able to make the default completion command hidden.
I may have gone too far with this PR so I'm looking for opinions 🤷‍♂️

This PR goes much beyond the ask and makes the completion command and it's four sub-commands fully accessible so the program can set whatever field it wants before calling rootCmd.Execute(). For example, a program could call

cobra.CompletionCmd.Hidden = true  // To make the command hidden

or/and

cobra.CompletionCmd.Use = "shellcomplete" // To rename the command

or/and

cobra.PwshCompletionCmd.Use = "pwsh"  // To use 'pwsh' instead of 'powershell' as a sub-command

How the new logic works:

1- the default completion command and its 4 sub-commands are now public global variables
2- at initialization the fields of these global variables that don't need special info are initialized (cmd.Use, cmd.Short, cmd.Args, cmd.ValidArgsFunction)
3- during rootCmd.Execute() the function cmd.initDefaultCompletionCmd() is called like before and initializes the remaining fields (these fields needed the program name and root command: cmd.Long, cmd.RunE and the flag)

The magic is that between point 2 and point 3, a program can modify the fields of these commands since they are accessible.

If this brings in too much complexity, we could simply offer a new option in CompletionOptions to allow hiding the command.

I made the effort of trying to be as flexible as possible because we got PRs that needed to change the descriptions and such which were slightly off. With this PR consumers should be able to adjust anything themselves. But it is more complex. And I had to do a little bit of resetting things so that go tests, which call rootCmd.Execute() multiple times would continue work; this is something we already do for the help command however, as we can see here:

cobra/command.go

Line 1114 in f09e947

c.RemoveCommand(c.helpCommand)

Opinions please. 😄

P.S. No tests added yet.

Fixes spf13#1507

This allows a program to set whatever field it wants on the default
completion command before calling rootCmd.Execute().  For example,

  cobra.CompletionCmd.Hidden = true  // To make the command hidden

or/and

  cobra.CompletionCmd.Use = "shellcomplete" // To rename the command

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
@marckhouzam
Copy link
Collaborator Author

marckhouzam commented Nov 24, 2021

This is turning out to be more complicated than it's worth. It is causing problems with tests which would require Cobra to reset everything about the completion command between tests, which is a big hassle.

I'm abandoning this attempt and have replaced it with #1541 which simply adds a user option to allow hiding the default completion command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hide auto-generated completion command
1 participant