-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add default completion command even if there are no other sub-commands #1559
base: main
Are you sure you want to change the base?
Conversation
Yep, seems to work nicely, thanks! An observation is that as the |
Nice attention to detail! The completions should work for hidden commands. In this case however, when we call the "__complete" command to get the completions, we don't create the "completion" command so its subcommands don't get completed. Let me try to see if I can fix it. |
This PR is being marked as stale due to a long period of inactivity |
This looks good, was just about to dig into this myself. Thanks! |
I've let this rot for too long. Let's plan it for the next release. Come August iI will run some tests on it and make sure it is good. |
anything I can do to help getting this merged? |
bc8a1d0
to
10702a4
Compare
10702a4
to
d1660cf
Compare
I've finally re-worked this and I believe it is ready. |
d1660cf
to
b1acb50
Compare
@jpmcb When you have moment, this is ready for review. Don't hesitate to ask for clarifications. |
When a program has no sub-commands, its root command can accept arguments. If we add the default "completion" command to such programs they will now have a sub-command and will no longer accept arguments. What we do instead for this special case, is only add the "completion" command if it is being called, or if it is being completed itself. We want to have the "completion" command for such programs because it will allow the completion of flags and of arguments (if provided by the program). Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
b1acb50
to
7260da1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -711,8 +711,8 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p | |||
// 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() { | |||
if c.CompletionOptions.DisableDefaultCmd || !c.HasSubCommands() { | |||
func (c *Command) InitDefaultCompletionCmd(args []string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering what's the impact of this now taking a slice of args? Can this be made optional to still be backwards compatible for users who've adopted InitDefaultCompletionCmd
? If it's fixing a bug / undefined behavior, that should be fine!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good point. I don’t recall why this function was ever exported. But since cobra does not need to be exported, it must mean that we did that for other people to call it. So good call that this is a breaking change.
we could simply make the argument an ellipsis to avoid breaking compilation. However, I feel this would be worse for programs that don’t have sub commands because we will break their behaviour because this completion command will now appear unless they pass the arguments properly.
I could create a new function instead I guess. I’ll give it some thought and post an update
Thanks for catching this
It’s the default “completion” command that doesn’t get created unless other commands exist. Say you write a tool called This PR makes this happen for tools that don’t have any sub commands |
This supersedes #1450 and #1392
When a program has no sub-commands, its root command can accept arguments. If we add the default "completion" command to such programs they will now have a sub-command and will no longer accept arguments.
What we do instead for this special case, is only add the "completion" command if it is being called.
We want to have the "completion" command for such programs because it will allow the completion of flags and of arguments (if provided by the program).
@scop can you confirm this is what you were hoping for?
Testing done