@@ -25,14 +25,25 @@ type ContextExecutor interface {
25
25
ExecuteContext (context.Context ) error
26
26
}
27
27
28
- // NewCommand builds the command context.
29
- func NewCommand (cliargs []string ) ContextExecutor {
28
+ // New builds the command context.
29
+ func New (cliargs []string ) ContextExecutor {
30
30
args := & Args {}
31
+ var (
32
+ bashCompletion bool
33
+ zshCompletion bool
34
+ fishCompletion bool
35
+ powershellCompletion bool
36
+ noDescriptions bool
37
+ )
31
38
v := viper .New ()
32
39
c := & cobra.Command {
33
- Use : text .CommandName + " [flags]... [DSN]" ,
34
- Short : text .Short (),
35
- Version : text .CommandVersion ,
40
+ Use : text .CommandName + " [flags]... [DSN]" ,
41
+ Short : text .Short (),
42
+ Version : text .CommandVersion ,
43
+ SilenceErrors : true ,
44
+ SilenceUsage : true ,
45
+ DisableAutoGenTag : true ,
46
+ DisableSuggestions : true ,
36
47
Args : func (_ * cobra.Command , cliargs []string ) error {
37
48
if len (cliargs ) > 1 {
38
49
return text .ErrWrongNumberOfArguments
@@ -74,6 +85,22 @@ func NewCommand(cliargs []string) ContextExecutor {
74
85
return nil
75
86
},
76
87
RunE : func (cmd * cobra.Command , cliargs []string ) error {
88
+ switch {
89
+ case bashCompletion :
90
+ return cmd .GenBashCompletionV2 (os .Stdout , ! noDescriptions )
91
+ case zshCompletion :
92
+ if noDescriptions {
93
+ return cmd .GenZshCompletionNoDesc (os .Stdout )
94
+ }
95
+ return cmd .GenZshCompletion (os .Stdout )
96
+ case fishCompletion :
97
+ return cmd .GenFishCompletion (os .Stdout , ! noDescriptions )
98
+ case powershellCompletion :
99
+ if noDescriptions {
100
+ return cmd .GenPowerShellCompletion (os .Stdout )
101
+ }
102
+ return cmd .GenPowerShellCompletionWithDesc (os .Stdout )
103
+ }
77
104
if len (cliargs ) > 0 {
78
105
args .DSN = cliargs [0 ]
79
106
}
@@ -83,10 +110,17 @@ func NewCommand(cliargs []string) ContextExecutor {
83
110
84
111
c .SetVersionTemplate ("{{ .Name }} {{ .Version }}\n " )
85
112
c .SetArgs (cliargs [1 :])
86
- c .SilenceUsage , c .SilenceErrors = true , true
87
113
c .SetUsageTemplate (text .UsageTemplate )
88
114
89
115
flags := c .Flags ()
116
+
117
+ // completions
118
+ flags .BoolVar (& bashCompletion , "completion-script-bash" , false , "output bash completion script and exit" )
119
+ flags .BoolVar (& zshCompletion , "completion-script-zsh" , false , "output zsh completion script and exit" )
120
+ flags .BoolVar (& fishCompletion , "completion-script-fish" , false , "output fish completion script and exit" )
121
+ flags .BoolVar (& powershellCompletion , "completion-script-powershell" , false , "output powershell completion script and exit" )
122
+ flags .BoolVar (& noDescriptions , "no-descriptions" , false , "disable descriptions in completion scripts" )
123
+
90
124
flags .SortFlags = false
91
125
// command / file flags
92
126
flags .VarP (commandOrFile {args , true }, "command" , "c" , "run only single command (SQL or internal) and exit" )
@@ -145,8 +179,12 @@ func NewCommand(cliargs []string) ContextExecutor {
145
179
_ , _ = w .Write ([]byte (s ))
146
180
}
147
181
// mark hidden
148
- for _ , s := range []string {"no-psqlrc" , "no-usqlrc" , "var" , "variable" } {
149
- if err := flags .MarkHidden (s ); err != nil {
182
+ for _ , name := range []string {
183
+ "no-psqlrc" , "no-usqlrc" , "var" , "variable" ,
184
+ "completion-script-bash" , "completion-script-zsh" , "completion-script-fish" ,
185
+ "completion-script-powershell" , "no-descriptions" ,
186
+ } {
187
+ if err := flags .MarkHidden (name ); err != nil {
150
188
panic (err )
151
189
}
152
190
}
@@ -313,7 +351,7 @@ func (c commandOrFile) Type() string {
313
351
return "FILE"
314
352
}
315
353
316
- // vs handles setting vars with predefined var names .
354
+ // vs handles setting vars with predefined values .
317
355
type vs struct {
318
356
vars * []string
319
357
vals []string
0 commit comments