-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var completionCmd = &cobra.Command{ | ||
Use: "completion [bash|zsh|fish|powershell]", | ||
Short: "Generate completion script", | ||
Long: `To load completions: | ||
Bash: | ||
$ source <(yourprogram completion bash) | ||
# To load completions for each session, execute once: | ||
# Linux: | ||
$ yourprogram completion bash > /etc/bash_completion.d/yourprogram | ||
# macOS: | ||
$ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram | ||
Zsh: | ||
# If shell completion is not already enabled in your environment, | ||
# you will need to enable it. You can execute the following once: | ||
$ echo "autoload -U compinit; compinit" >> ~/.zshrc | ||
# To load completions for each session, execute once: | ||
$ yourprogram completion zsh > "${fpath[1]}/_yourprogram" | ||
# You will need to start a new shell for this setup to take effect. | ||
fish: | ||
$ yourprogram completion fish | source | ||
# To load completions for each session, execute once: | ||
$ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish | ||
PowerShell: | ||
PS> yourprogram completion powershell | Out-String | Invoke-Expression | ||
# To load completions for every new session, run: | ||
PS> yourprogram completion powershell > yourprogram.ps1 | ||
# and source this file from your PowerShell profile. | ||
`, | ||
DisableFlagsInUseLine: true, | ||
ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, | ||
Args: cobra.ExactValidArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
switch args[0] { | ||
case "bash": | ||
return cmd.Root().GenBashCompletion(os.Stdout) | ||
case "zsh": | ||
return cmd.Root().GenZshCompletion(os.Stdout) | ||
case "fish": | ||
return cmd.Root().GenFishCompletion(os.Stdout, true) | ||
case "powershell": | ||
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(completionCmd) | ||
} |