Skip to content

Commit 4d544eb

Browse files
authored
print command line completions (#337) (#606)
1 parent 3977701 commit 4d544eb

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

main.go

+27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package main
44

55
import (
6+
"errors"
67
"fmt"
78
"os"
89
"runtime"
@@ -12,6 +13,21 @@ import (
1213
"github.com/spf13/cobra"
1314
)
1415

16+
func printShellCompletion(cmd *cobra.Command, command string) error {
17+
switch command {
18+
case "bash":
19+
return cmd.GenBashCompletionV2(os.Stdout, true)
20+
case "zsh":
21+
return cmd.GenZshCompletion(os.Stdout)
22+
case "fish":
23+
return cmd.GenFishCompletion(os.Stdout, true)
24+
case "powershell":
25+
return cmd.GenPowerShellCompletion(os.Stdout)
26+
default:
27+
return errors.New("Unknown shell: " + command)
28+
}
29+
}
30+
1531
//go:generate go run scripts/include.go
1632
func main() {
1733
// f, _ := os.Create("scc.pprof")
@@ -426,6 +442,17 @@ func main() {
426442
"set currency symbol",
427443
)
428444

445+
// If invoked in the format of "scc completion --shell [name of shell]", generate command line completions instead.
446+
// With the --shell option, unintentionally triggering shell completions should be highly unlikely.
447+
args := os.Args
448+
if len(args) == 4 && args[1] == "completion" && args[2] == "--shell" {
449+
err := printShellCompletion(rootCmd, args[3])
450+
if err != nil {
451+
fmt.Printf("Error printing shell completion: %s\n", err)
452+
}
453+
return
454+
}
455+
429456
if err := rootCmd.Execute(); err != nil {
430457
os.Exit(1)
431458
}

0 commit comments

Comments
 (0)