package cmd import ( "os" "github.com/spf13/cobra" ) var completionCmd = &cobra.Command{ Use: "completion [bash|zsh|fish|powershell]", Short: "Generate shell completion scripts", Long: `Generate shell completion scripts for cacher. To load completions: Bash: $ source <(cacher completion bash) # To load completions for each session, execute once: # Linux: $ cacher completion bash > /etc/bash_completion.d/cacher # macOS: $ cacher completion bash > $(brew --prefix)/etc/bash_completion.d/cacher Zsh: $ source <(cacher completion zsh) # To load completions for each session, execute once: $ cacher completion zsh > "${fpath[1]}/_cacher" Fish: $ cacher completion fish | source # To load completions for each session, execute once: $ cacher completion fish > ~/.config/fish/completions/cacher.fish PowerShell: PS> cacher completion powershell | Out-String | Invoke-Expression `, DisableFlagsInUseLine: true, ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), RunE: func(cmd *cobra.Command, args []string) error { switch args[0] { case "bash": return rootCmd.GenBashCompletion(os.Stdout) case "zsh": return rootCmd.GenZshCompletion(os.Stdout) case "fish": return rootCmd.GenFishCompletion(os.Stdout, true) case "powershell": return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout) } return nil }, } func init() { rootCmd.AddCommand(completionCmd) }