Skip to content

Commit 9c39122

Browse files
committed
feat: add completion command to root command
- Add `CompletionCmd` to the `rootCmd` in `cmd/cmd.go` - Add a new file `cmd/completion.go` with the completion command implementation Signed-off-by: appleboy <[email protected]>
1 parent 4029bb7 commit 9c39122

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func init() {
3939
rootCmd.AddCommand(commitCmd)
4040
rootCmd.AddCommand(hookCmd)
4141
rootCmd.AddCommand(reviewCmd)
42+
rootCmd.AddCommand(CompletionCmd)
4243

4344
// hide completion command
4445
rootCmd.CompletionOptions.HiddenDefaultCmd = true

cmd/completion.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
DisableFlagsInUseLine: true,
14+
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
15+
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
16+
Run: func(cmd *cobra.Command, args []string) {
17+
switch args[0] {
18+
case "bash":
19+
cmd.Root().GenBashCompletion(os.Stdout)
20+
case "zsh":
21+
cmd.Root().GenZshCompletion(os.Stdout)
22+
case "fish":
23+
cmd.Root().GenFishCompletion(os.Stdout, true)
24+
case "powershell":
25+
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
26+
}
27+
},
28+
}

0 commit comments

Comments
 (0)