-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Filter root command completion on condition #2262
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
Comments
Update. I see now its been triggered. So its only added new command |
Ok. More updates. I thought I will leave the completion logic for now and use the PersistentPreRunE. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := util.CheckSomeLogic(); err != nil {
return fmt.Errorf("%v", err)
}
return nil
}, With this the completions not working. So I guess the PersistentPreRunE is executed on the completion?(TAB) It printed the usage of the package. I think its not convenient. I would like to see only my error and that it. Is it possible? |
Cobra will automatically complete all the available commands. So, you can try in the PersistentPreRun:
If this does not work in the PersistentPreRun (I'm not sure without trying), then you can do something similar before |
Try setting |
This one works like a charm: if err := util.CheckMyInitLogic(); err == nil {
rootCmd.AddCommand(commands.())
rootCmd.AddCommand(commands.())
...
} else {
rootCmd.AddCommand(commands.NewInitCommand())
}
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
} This one works. But now when I am trying to execute a legal command that must work after init returns: ~% my_cli_tool some-command
Error: unknown command "some-command" for "my_cli_tool"
Run 'my_cli_tool --help' for usage. So I guess its not very good UX. My initial intention was to inform the user to execute the init command before he can use other commands. |
Instead of not creating the commands, you can instead make them hidden, which means they can be invoked and you won't get the error. However, if you run So what you should do is
So, the commands will all exist all the time, except when you try to complete them |
Ok. I am trying to understand. But the issue I encountered with ValidArgsFunction is that it only adds new completions when I am trying to use it on root command. |
Yeah, it's hard to explain in a comment. But let's try to figure this out.
Except when the command being called is So, either in the PersistenPreRun, or if it does not work, before root.Execute is called, you will need to:
At that point it is too late because Cobra has already built a list of all existing commands and will suggest them to the user. |
Thanks for your help and patience. if cmd.Name() == cobra.ShellCompRequestCmd {
^^^ <<---- How do I get this?
// ShellCompRequestCmd is for __complete
// What is for __completeNoDesc?
if err := util.CheckRepoContext(); err != nil {
cmd.Root().RemoveCommand(commands.NewAddRequestCommand())
cmd.Root().RemoveCommand(commands.NewRequestCommand())
cmd.Root().RemoveCommand(commands.NewSetEnvCommand())
cmd.Root().RemoveCommand(commands.NewSetEnvVarCommand())
cmd.Root().RemoveCommand(commands.NewShowEnvCommand())
}
}
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
} |
You’ll have to use |
PreRun won’t work for sure. Try PersistentPreRun, although I’m not sure |
I have an init command in my tool.
I want to do some logical checks on the directory where the command was executed
And if it meets specific criteria to show the completion of all the commands and if not to show only the init command.
Is there a way to do it with Cobra today?
I tried to use ValidArgsFunc on the root command but I see it's not been triggered.
The text was updated successfully, but these errors were encountered: