Skip to content

Commit 85fc4ce

Browse files
committed
Disabling -h manually
1 parent 1cca4d7 commit 85fc4ce

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

run.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"os"
@@ -34,6 +35,7 @@ func New(cliargs []string) ContextExecutor {
3435
fishCompletion bool
3536
powershellCompletion bool
3637
noDescriptions bool
38+
badHelp bool
3739
)
3840
v := viper.New()
3941
c := &cobra.Command{
@@ -85,6 +87,7 @@ func New(cliargs []string) ContextExecutor {
8587
return nil
8688
},
8789
RunE: func(cmd *cobra.Command, cliargs []string) error {
90+
// completions
8891
switch {
8992
case bashCompletion:
9093
return cmd.GenBashCompletionV2(os.Stdout, !noDescriptions)
@@ -100,7 +103,11 @@ func New(cliargs []string) ContextExecutor {
100103
return cmd.GenPowerShellCompletion(os.Stdout)
101104
}
102105
return cmd.GenPowerShellCompletionWithDesc(os.Stdout)
106+
case badHelp:
107+
return errors.New("unknown shorthand flag: 'h' in -h")
103108
}
109+
110+
// run
104111
if len(cliargs) > 0 {
105112
args.DSN = cliargs[0]
106113
}
@@ -113,18 +120,20 @@ func New(cliargs []string) ContextExecutor {
113120
c.SetUsageTemplate(text.UsageTemplate)
114121

115122
flags := c.Flags()
123+
flags.SortFlags = false
116124

117-
// completions
125+
// completions / short circuits
118126
flags.BoolVar(&bashCompletion, "completion-script-bash", false, "output bash completion script and exit")
119127
flags.BoolVar(&zshCompletion, "completion-script-zsh", false, "output zsh completion script and exit")
120128
flags.BoolVar(&fishCompletion, "completion-script-fish", false, "output fish completion script and exit")
121129
flags.BoolVar(&powershellCompletion, "completion-script-powershell", false, "output powershell completion script and exit")
122130
flags.BoolVar(&noDescriptions, "no-descriptions", false, "disable descriptions in completion scripts")
131+
flags.BoolVarP(&badHelp, "bad-help", "h", false, "bad help")
123132

124-
flags.SortFlags = false
125133
// command / file flags
126134
flags.VarP(commandOrFile{args, true}, "command", "c", "run only single command (SQL or internal) and exit")
127135
flags.VarP(commandOrFile{args, false}, "file", "f", "execute commands from file and exit")
136+
128137
// general flags
129138
flags.BoolVarP(&args.NoPassword, "no-password", "w", false, "never prompt for password")
130139
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 {
162171
ss(&args.PVariables, "vertical", "G", "vertical output mode", "", "format=vertical")
163172
// set bools
164173
ss(&args.Variables, "quiet", "q", "run quietly (no messages, only query output)", "", "QUIET=on")
174+
165175
// add config
166176
_ = flags.StringP("config", "", "", "config file")
177+
167178
// 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")
169180
_ = flags.SetAnnotation("version", cobra.FlagSetByCobraAnnotation, []string{"true"})
181+
170182
// 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")
172184
_ = c.Flags().SetAnnotation("help", cobra.FlagSetByCobraAnnotation, []string{"true"})
185+
173186
// expose to metacmd
174187
metacmd.Usage = func(w io.Writer, banner bool) {
175-
s := "\n\n" + c.UsageString()
188+
s := c.UsageString()
176189
if banner {
177-
s = text.Short() + s
190+
s = text.Short() + "\n\n" + s
178191
}
179192
_, _ = w.Write([]byte(s))
180193
}
194+
181195
// mark hidden
182196
for _, name := range []string{
183197
"no-psqlrc", "no-usqlrc", "var", "variable",
184198
"completion-script-bash", "completion-script-zsh", "completion-script-fish",
185199
"completion-script-powershell", "no-descriptions",
200+
"bad-help",
186201
} {
187202
if err := flags.MarkHidden(name); err != nil {
188203
panic(err)

0 commit comments

Comments
 (0)