Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/config/completions.go
2649 views
1
package config
2
3
import (
4
"fmt"
5
"os"
6
7
"github.com/spf13/cobra"
8
)
9
10
func GenCompletions(command *cobra.Command, shell string) error {
11
12
var completionCmd = &cobra.Command{
13
Use: "chatgpt --set-completions [bash|zsh|fish|powershell]",
14
Short: "Generate completion script",
15
Long: `To load completions:
16
17
Bash:
18
19
$ source <(chatgpt --set-completions bash)
20
21
# To load completions for each session, execute once:
22
# Linux:
23
$ chatgpt --set-completions bash > /etc/bash_completion.d/chatgpt
24
# macOS:
25
$ chatgpt --set-completions bash > /usr/local/etc/bash_completion.d/chatgpt
26
27
Zsh:
28
29
# If shell completion is not already enabled in your environment,
30
# you will need to enable it. You can execute the following once:
31
32
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
33
34
# To load completions for each session, execute once:
35
$ chatgpt --set-completions zsh > "${fpath[1]}/_chatgpt"
36
37
# You will need to start a new shell for this setup to take effect.
38
39
fish:
40
41
$ chatgpt --set-completions fish | source
42
43
# To load completions for each session, execute once:
44
$ chatgpt --set-completions fish > ~/.config/fish/completions/chatgpt.fish
45
46
PowerShell:
47
48
PS> chatgpt --set-completions powershell | Out-String | Invoke-Expression
49
50
# To load completions for every new session, run:
51
PS> chatgpt --set-completions powershell > chatgpt.ps1
52
# and source this file from your PowerShell profile.
53
`,
54
Args: cobra.ExactArgs(1),
55
DisableFlagsInUseLine: true,
56
DisableFlagParsing: true,
57
Run: func(cmd *cobra.Command, args []string) {
58
switch args[0] {
59
case "bash":
60
_ = command.Root().GenBashCompletion(os.Stdout)
61
case "zsh":
62
_ = command.Root().GenZshCompletion(os.Stdout)
63
case "fish":
64
_ = command.Root().GenFishCompletion(os.Stdout, true)
65
case "powershell":
66
_ = command.Root().GenPowerShellCompletionWithDesc(os.Stdout)
67
case "-h", "--help":
68
_ = cmd.Help()
69
default:
70
fmt.Printf(`
71
Usage:
72
chatgpt --set-completions [bash|zsh|fish|powershell]
73
74
Flags:
75
-h, --help help for --set-completions
76
77
Invalid Arg: %s
78
`, args[0])
79
}
80
},
81
}
82
completionCmd.SetArgs([]string{shell})
83
return completionCmd.Execute()
84
}
85
86