Skip to content

Commit 30d3caf

Browse files
committed
feat: implement completion command
1 parent f0a1dcc commit 30d3caf

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

cmd/completion.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 <(deck completion bash)
17+
18+
# To load completions for each session, execute once:
19+
# Linux:
20+
$ deck completion bash > /etc/bash_completion.d/deck
21+
# macOS:
22+
$ deck completion bash > /usr/local/etc/bash_completion.d/deck
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+
$ deck 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+
$ deck completion fish | source
39+
40+
# To load completions for each session, execute once:
41+
$ deck completion fish > ~/.config/fish/completions/deck.fish
42+
43+
PowerShell:
44+
45+
PS> deck completion powershell | Out-String | Invoke-Expression
46+
47+
# To load completions for every new session, run:
48+
PS> deck completion powershell > deck.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

Comments
 (0)