Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Parse list flags correctly #543

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading