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

Question: complement debug give correct output, but the normal usage fails #1264

Closed
Yiyiyimu opened this issue Oct 24, 2020 · 2 comments
Closed

Comments

@Yiyiyimu
Copy link

Yiyiyimu commented Oct 24, 2020

Hi community,

Thanks for the work, it's brilliant! My main part of code would be like

func init() {
	o := &DebugOptions{}
	validArgs := []string{"networkchaos", "stresschaos"}

	var debugCmd = &cobra.Command{
		Use:   `debug (CHAOSTYPE) [-c CHAOSNAME] [-n NAMESPACE]`,
		Short: `Print the debug information for certain chaos`,
		Long: `Print the debug information for certain chaos`
	}
	debugCmd.Flags().StringVarP(&o.Namespace, "namespace", "n", "default", "namespace to find chaos")

	if err := flagCompletion(debugCmd); err != nil {
		log.Fatal(err)
	}

	rootCmd.AddCommand(debugCmd)
}

And I use the default template for completion.go in shell-completion

So there would be two part of completion, one for noun and another one for flags. Both were tested correctly using debug like

~ chaosctl __complete debug -n ""
chaos-testing
default
kube-node-lease
kube-public
kube-system
local-path-storage
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

~ chaosctl __complete debug ""
networkchaos
stresschaos
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

And since I'm using zsh, I do source <(chaosctl completion zsh) to activate completion. But when I really use it, the completion would just showing files and dirs, so it seems not working. However when I use the same method to activate completion for helm, everything works fine.

Is there any way to test if completion is working?

cobra version: v1.1.0

@marckhouzam
Copy link
Collaborator

Hi @Yiyiyimu.
The standard Cobra zsh completion does not support sourcing the script because zsh does not recommend that approach. What you must do is store the script under a directory that is in you $fpath.

As for Helm, it extends the script to support source to remain backwards compatible (one extra line). You can try doing it by hand.

In your shell, run:
compdef _chaosctl chaosctl
Then completion should work.

@Yiyiyimu
Copy link
Author

It works following your suggestion! Thank you Marc!

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

No branches or pull requests

2 participants