@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"io"
7
8
"os"
@@ -34,6 +35,7 @@ func New(cliargs []string) ContextExecutor {
34
35
fishCompletion bool
35
36
powershellCompletion bool
36
37
noDescriptions bool
38
+ badHelp bool
37
39
)
38
40
v := viper .New ()
39
41
c := & cobra.Command {
@@ -85,6 +87,7 @@ func New(cliargs []string) ContextExecutor {
85
87
return nil
86
88
},
87
89
RunE : func (cmd * cobra.Command , cliargs []string ) error {
90
+ // completions
88
91
switch {
89
92
case bashCompletion :
90
93
return cmd .GenBashCompletionV2 (os .Stdout , ! noDescriptions )
@@ -100,7 +103,11 @@ func New(cliargs []string) ContextExecutor {
100
103
return cmd .GenPowerShellCompletion (os .Stdout )
101
104
}
102
105
return cmd .GenPowerShellCompletionWithDesc (os .Stdout )
106
+ case badHelp :
107
+ return errors .New ("unknown shorthand flag: 'h' in -h" )
103
108
}
109
+
110
+ // run
104
111
if len (cliargs ) > 0 {
105
112
args .DSN = cliargs [0 ]
106
113
}
@@ -113,18 +120,20 @@ func New(cliargs []string) ContextExecutor {
113
120
c .SetUsageTemplate (text .UsageTemplate )
114
121
115
122
flags := c .Flags ()
123
+ flags .SortFlags = false
116
124
117
- // completions
125
+ // completions / short circuits
118
126
flags .BoolVar (& bashCompletion , "completion-script-bash" , false , "output bash completion script and exit" )
119
127
flags .BoolVar (& zshCompletion , "completion-script-zsh" , false , "output zsh completion script and exit" )
120
128
flags .BoolVar (& fishCompletion , "completion-script-fish" , false , "output fish completion script and exit" )
121
129
flags .BoolVar (& powershellCompletion , "completion-script-powershell" , false , "output powershell completion script and exit" )
122
130
flags .BoolVar (& noDescriptions , "no-descriptions" , false , "disable descriptions in completion scripts" )
131
+ flags .BoolVarP (& badHelp , "bad-help" , "h" , false , "bad help" )
123
132
124
- flags .SortFlags = false
125
133
// command / file flags
126
134
flags .VarP (commandOrFile {args , true }, "command" , "c" , "run only single command (SQL or internal) and exit" )
127
135
flags .VarP (commandOrFile {args , false }, "file" , "f" , "execute commands from file and exit" )
136
+
128
137
// general flags
129
138
flags .BoolVarP (& args .NoPassword , "no-password" , "w" , false , "never prompt for password" )
130
139
flags .BoolVarP (& args .NoRC , "no-rc" , "X" , false , "do not read start up file (aliases: --no-psqlrc --no-usqlrc)" )
@@ -162,27 +171,33 @@ func New(cliargs []string) ContextExecutor {
162
171
ss (& args .PVariables , "vertical" , "G" , "vertical output mode" , "" , "format=vertical" )
163
172
// set bools
164
173
ss (& args .Variables , "quiet" , "q" , "run quietly (no messages, only query output)" , "" , "QUIET=on" )
174
+
165
175
// add config
166
176
_ = flags .StringP ("config" , "" , "" , "config file" )
177
+
167
178
// manually set --version, see github.com/spf13/cobra/command.go
168
- flags .BoolP ("version" , "V" , false , "output version information, then exit" )
179
+ _ = flags .BoolP ("version" , "V" , false , "output version information, then exit" )
169
180
_ = flags .SetAnnotation ("version" , cobra .FlagSetByCobraAnnotation , []string {"true" })
181
+
170
182
// manually set --help, see github.com/spf13/cobra/command.go
171
- flags .Bool ("help" , false , "show this help, then exit" )
183
+ _ = flags .BoolP ("help" , "? " , false , "show this help, then exit" )
172
184
_ = c .Flags ().SetAnnotation ("help" , cobra .FlagSetByCobraAnnotation , []string {"true" })
185
+
173
186
// expose to metacmd
174
187
metacmd .Usage = func (w io.Writer , banner bool ) {
175
- s := " \n \n " + c .UsageString ()
188
+ s := c .UsageString ()
176
189
if banner {
177
- s = text .Short () + s
190
+ s = text .Short () + " \n \n " + s
178
191
}
179
192
_ , _ = w .Write ([]byte (s ))
180
193
}
194
+
181
195
// mark hidden
182
196
for _ , name := range []string {
183
197
"no-psqlrc" , "no-usqlrc" , "var" , "variable" ,
184
198
"completion-script-bash" , "completion-script-zsh" , "completion-script-fish" ,
185
199
"completion-script-powershell" , "no-descriptions" ,
200
+ "bad-help" ,
186
201
} {
187
202
if err := flags .MarkHidden (name ); err != nil {
188
203
panic (err )
0 commit comments