Skip to content

Commit c702a83

Browse files
fix: help flags preprocessing for -h and -help (#707)
1 parent 8ae5174 commit c702a83

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

cmd/sqlcmd/sqlcmd.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ func Execute(version string) {
295295
// We need to rewrite the arguments to add -i and -v in front of each space-delimited value to be Cobra-friendly.
296296
// For flags like -r we need to inject the default value if the user omits it
297297
func convertOsArgs(args []string) (cargs []string) {
298+
args = preprocessHelpFlags(args)
299+
298300
flag := ""
299301
first := true
300302
for i, a := range args {
@@ -324,6 +326,31 @@ func convertOsArgs(args []string) (cargs []string) {
324326
return
325327
}
326328

329+
// preprocessHelpFlags converts -h (without number) and -help to help flags.
330+
// -h with a number is left alone for header count backward compatibility.
331+
func preprocessHelpFlags(args []string) []string {
332+
result := make([]string, 0, len(args))
333+
for i := 0; i < len(args); i++ {
334+
arg := args[i]
335+
if arg == "-help" {
336+
result = append(result, "--help")
337+
continue
338+
}
339+
if arg == "-h" {
340+
if i+1 < len(args) {
341+
if _, err := strconv.Atoi(args[i+1]); err == nil {
342+
result = append(result, arg) // -h <number> for headers
343+
continue
344+
}
345+
}
346+
result = append(result, "-?")
347+
continue
348+
}
349+
result = append(result, arg)
350+
}
351+
return result
352+
}
353+
327354
// If args[i] is the given flag and args[i+1] is another flag, returns the value to append after the flag
328355
func checkDefaultValue(args []string, i int) (val string) {
329356
flags := map[rune]string{

cmd/sqlcmd/sqlcmd_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,30 @@ func TestConvertOsArgs(t *testing.T) {
593593
}
594594
}
595595

596+
func TestPreprocessHelpFlags(t *testing.T) {
597+
type test struct {
598+
name string
599+
in []string
600+
expected []string
601+
}
602+
603+
tests := []test{
604+
{"empty args", []string{}, []string{}},
605+
{"-help to --help", []string{"-help"}, []string{"--help"}},
606+
{"-h alone to -?", []string{"-h"}, []string{"-?"}},
607+
{"-h with number stays", []string{"-h", "5"}, []string{"-h", "5"}},
608+
{"-h with flag becomes -?", []string{"-h", "-S"}, []string{"-?", "-S"}},
609+
{"mixed args", []string{"-S", "server", "-h", "-Q", "select 1"}, []string{"-S", "server", "-?", "-Q", "select 1"}},
610+
{"preserve -h N in context", []string{"-S", "srv", "-h", "10", "-Q", "x"}, []string{"-S", "srv", "-h", "10", "-Q", "x"}},
611+
}
612+
for _, c := range tests {
613+
t.Run(c.name, func(t *testing.T) {
614+
actual := preprocessHelpFlags(c.in)
615+
assert.Equal(t, c.expected, actual, "Incorrect preprocessed args")
616+
})
617+
}
618+
}
619+
596620
func TestEncryptionOptions(t *testing.T) {
597621
type test struct {
598622
input string

0 commit comments

Comments
 (0)