|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | +) |
| 8 | + |
| 9 | +var completionCmd = &cobra.Command{ |
| 10 | + Use: "completion [bash|zsh|fish|powershell]", |
| 11 | + Short: "Generate completion script", |
| 12 | + Long: `To load completions: |
| 13 | +
|
| 14 | +Bash: |
| 15 | +
|
| 16 | + $ source <(yourprogram completion bash) |
| 17 | +
|
| 18 | + # To load completions for each session, execute once: |
| 19 | + # Linux: |
| 20 | + $ yourprogram completion bash > /etc/bash_completion.d/yourprogram |
| 21 | + # macOS: |
| 22 | + $ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram |
| 23 | +
|
| 24 | +Zsh: |
| 25 | +
|
| 26 | + # If shell completion is not already enabled in your environment, |
| 27 | + # you will need to enable it. You can execute the following once: |
| 28 | +
|
| 29 | + $ echo "autoload -U compinit; compinit" >> ~/.zshrc |
| 30 | +
|
| 31 | + # To load completions for each session, execute once: |
| 32 | + $ yourprogram completion zsh > "${fpath[1]}/_yourprogram" |
| 33 | +
|
| 34 | + # You will need to start a new shell for this setup to take effect. |
| 35 | +
|
| 36 | +fish: |
| 37 | +
|
| 38 | + $ yourprogram completion fish | source |
| 39 | +
|
| 40 | + # To load completions for each session, execute once: |
| 41 | + $ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish |
| 42 | +
|
| 43 | +PowerShell: |
| 44 | +
|
| 45 | + PS> yourprogram completion powershell | Out-String | Invoke-Expression |
| 46 | +
|
| 47 | + # To load completions for every new session, run: |
| 48 | + PS> yourprogram completion powershell > yourprogram.ps1 |
| 49 | + # and source this file from your PowerShell profile. |
| 50 | +`, |
| 51 | + DisableFlagsInUseLine: true, |
| 52 | + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, |
| 53 | + Args: cobra.ExactValidArgs(1), |
| 54 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 55 | + switch args[0] { |
| 56 | + case "bash": |
| 57 | + return cmd.Root().GenBashCompletion(os.Stdout) |
| 58 | + case "zsh": |
| 59 | + return cmd.Root().GenZshCompletion(os.Stdout) |
| 60 | + case "fish": |
| 61 | + return cmd.Root().GenFishCompletion(os.Stdout, true) |
| 62 | + case "powershell": |
| 63 | + return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) |
| 64 | + } |
| 65 | + return nil |
| 66 | + }, |
| 67 | +} |
| 68 | + |
| 69 | +func init() { |
| 70 | + rootCmd.AddCommand(completionCmd) |
| 71 | +} |
0 commit comments