completion.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package cmd
  2. import (
  3. "os"
  4. "github.com/spf13/cobra"
  5. )
  6. // completionCmd represents the completion command
  7. var completionCmd = &cobra.Command{
  8. Use: "completion [bash|zsh|fish|powershell]",
  9. Short: "Generate completion script",
  10. Long: `To load completions:
  11. Bash:
  12. $ source <(qrcp completion bash)
  13. # To load completions for each session, execute once:
  14. Linux:
  15. $ qrcp completion bash > /etc/bash_completion.d/qrcp
  16. MacOS:
  17. $ qrcp completion bash > /usr/local/etc/bash_completion.d/qrcp
  18. Zsh:
  19. # If shell completion is not already enabled in your environment you will need
  20. # to enable it. You can execute the following once:
  21. $ echo "autoload -U compinit; compinit" >> ~/.zshrc
  22. # To load completions for each session, execute once:
  23. $ qrcp completion zsh > "${fpath[1]}/_qrcp"
  24. # You will need to start a new shell for this setup to take effect.
  25. Fish:
  26. $ qrcp completion fish | source
  27. # To load completions for each session, execute once:
  28. $ qrcp completion fish > ~/.config/fish/completions/qrcp.fish
  29. `,
  30. DisableFlagsInUseLine: true,
  31. ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
  32. Args: cobra.ExactValidArgs(1),
  33. Run: func(cmd *cobra.Command, args []string) {
  34. switch args[0] {
  35. case "bash":
  36. if err := cmd.Root().GenBashCompletion(os.Stdout); err != nil {
  37. panic(err)
  38. }
  39. case "zsh":
  40. if err := cmd.Root().GenZshCompletion(os.Stdout); err != nil {
  41. panic(err)
  42. }
  43. case "fish":
  44. if err := cmd.Root().GenFishCompletion(os.Stdout, true); err != nil {
  45. panic(err)
  46. }
  47. case "powershell":
  48. if err := cmd.Root().GenPowerShellCompletion(os.Stdout); err != nil {
  49. panic(err)
  50. }
  51. }
  52. },
  53. }