Skip to content

Commit e3e40b3

Browse files
committed
support different shells for completion
1 parent d248a72 commit e3e40b3

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

cmd/devcontainer/completion.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
6+
"strings"
57

68
"github.com/spf13/cobra"
79
)
810

911
func createCompleteCommand(rootCmd *cobra.Command) *cobra.Command {
10-
return &cobra.Command{
11-
Use: "completion",
12+
cmd := &cobra.Command{
13+
Use: "completion SHELL",
1214
Short: "Generates bash completion scripts",
1315
Long: `To load completion run
1416
15-
. <(devcontainer completion)
17+
. <(devcontainer completion SHELL)
18+
19+
Valid values for SHELL are : bash, fish, powershell, zsh
1620
17-
To configure your bash shell to load completions for each session add to your bashrc
21+
For example, to configure your bash shell to load completions for each session add to your bashrc
1822
1923
# ~/.bashrc or ~/.profile
2024
. <(devcontainer completion)
2125
`,
2226
Run: func(cmd *cobra.Command, args []string) {
23-
rootCmd.GenBashCompletion(os.Stdout)
27+
if len(args) != 1 {
28+
cmd.Usage()
29+
os.Exit(1)
30+
}
31+
shell := args[0]
32+
switch strings.ToLower(shell) {
33+
case "bash":
34+
rootCmd.GenBashCompletion(os.Stdout)
35+
case "fish":
36+
rootCmd.GenFishCompletion(os.Stdout, true)
37+
case "powershell":
38+
rootCmd.GenPowerShellCompletion(os.Stdout)
39+
case "zsh":
40+
rootCmd.GenPowerShellCompletion(os.Stdout)
41+
default:
42+
fmt.Printf("Unsupported SHELL value: '%s'\n", shell)
43+
cmd.Usage()
44+
os.Exit(1)
45+
}
2446
},
2547
}
48+
return cmd
2649
}

0 commit comments

Comments
 (0)