Skip to content

Commit

Permalink
fix: Parse list flags correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
shueybubbles committed Jul 23, 2024
1 parent c7ab91d commit c2058d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cmd/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ func convertOsArgs(args []string) (cargs []string) {
}
var defValue string
if isListFlag(a) {
flag = a
first = true
flag = a[0:2]
first = len(a) == 2
} else {
defValue = checkDefaultValue(args, i)
}
Expand Down Expand Up @@ -326,11 +326,11 @@ func checkDefaultValue(args []string, i int) (val string) {
}

func isFlag(arg string) bool {
return len(arg) == 2 && arg[0] == '-'
return arg[0] == '-'
}

func isListFlag(arg string) bool {
return arg == "-v" || arg == "-i"
return len(arg) > 1 && (arg[0:2] == "-v" || arg[0:2] == "-i")
}

func formatDescription(description string, maxWidth, indentWidth int) string {
Expand Down
13 changes: 13 additions & 0 deletions cmd/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func TestValidCommandLineToArgsConversion(t *testing.T) {
{[]string{"-N", "m"}, func(args SQLCmdArguments) bool {
return args.EncryptConnection == "m"
}},
{[]string{"-ifoo.sql", "bar.sql", "-V10"}, func(args SQLCmdArguments) bool {
return args.ErrorSeverityLevel == 10 && args.InputFile[0] == "foo.sql" && args.InputFile[1] == "bar.sql"
}},
}

for _, test := range commands {
Expand Down Expand Up @@ -527,6 +530,16 @@ func TestConvertOsArgs(t *testing.T) {
[]string{"-r", "1", "-X", "-k", "-C"},
[]string{"-r", "1", "-X", "0", "-k", "0", "-C"},
},
{
"-i followed by flags without spaces",
[]string{"-i", "a.sql", "-V10", "-C"},
[]string{"-i", "a.sql", "-V10", "-C"},
},
{
"list flags without spaces",
[]string{"-ifoo.sql", "bar.sql", "-V10", "-X", "-va=b", "c=d"},
[]string{"-ifoo.sql", "-i", "bar.sql", "-V10", "-X", "0", "-va=b", "-v", "c=d"},
},
}
for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
Expand Down

0 comments on commit c2058d4

Please sign in to comment.