Skip to content

Commit 28d5857

Browse files
Consume non-numeric, non-flag arguments after -h when showing help
When -h is followed by a non-numeric argument that is not a flag (e.g., 'abc'), consume it to prevent "Unknown command" errors. Flags are preserved for normal processing (e.g., -h -E converts to -? -E, not just -?). This addresses PR review feedback to improve user experience. Co-authored-by: dlevy-msft-sql <[email protected]>
1 parent 63c01b1 commit 28d5857

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

cmd/sqlcmd/sqlcmd.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,30 @@ func preprocessHelpFlags(args []string) []string {
348348
// Check if next arg exists
349349
hasNextArg := i+1 < len(args)
350350
nextIsNumber := false
351+
nextIsFlag := false
351352
if hasNextArg {
353+
nextArg := args[i+1]
352354
// Check if next arg looks like a number (including negative numbers)
353355
// This must be checked before checking if it's a flag, because
354356
// negative numbers start with '-'
355-
_, err := strconv.Atoi(args[i+1])
357+
_, err := strconv.Atoi(nextArg)
356358
nextIsNumber = err == nil
359+
360+
// Check if next arg is a flag (starts with '-' but is not a number)
361+
if !nextIsNumber && len(nextArg) > 0 && nextArg[0] == '-' {
362+
nextIsFlag = true
363+
}
357364
}
358365

359366
// If -h is followed by a number, keep it as-is (header count)
360-
// Otherwise, convert to -? (show help)
367+
// Otherwise, convert to -? (show help) and consume any non-numeric, non-flag argument
361368
if !nextIsNumber {
362369
result = append(result, "-?")
370+
// If there was a following non-numeric, non-flag argument, consume it
371+
// (Don't consume flags, as they should be processed normally)
372+
if hasNextArg && !nextIsFlag {
373+
i++
374+
}
363375
} else {
364376
result = append(result, arg)
365377
}

cmd/sqlcmd/sqlcmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,9 @@ func TestConvertOsArgs(t *testing.T) {
619619
[]string{"-?", "-E"},
620620
},
621621
{
622-
"-h followed by non-numeric converts to -? for help",
622+
"-h followed by non-numeric converts to -? for help (consuming the non-numeric arg)",
623623
[]string{"-h", "abc"},
624-
[]string{"-?", "abc"},
624+
[]string{"-?"},
625625
},
626626
}
627627
for _, c := range tests {

0 commit comments

Comments
 (0)