Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 31, 2026

Fix -h and -help flags to show help instead of requiring arguments

sqlcmd -h returns "Missing argument" error because -h expects an integer for header count. Additionally, -? requires quoting in zsh due to glob expansion, making help discovery difficult.

Changes

Argument preprocessing in cmd/sqlcmd/sqlcmd.go:

  • Added preprocessHelpFlags() that converts -h to -? when not followed by a number
  • Converts -help to --help (avoids shell parsing issues)
  • Preserves -h <number> for backward compatibility with header count flag
  • NEW: Consumes non-numeric, non-flag arguments after -h to prevent "Unknown command" errors
// Before: sqlcmd -h → "Missing argument" error
//         sqlcmd -h abc → "Unknown command: abc" error
// After:  sqlcmd -h → shows help
//         sqlcmd -h abc → shows help (consumes 'abc')
//         sqlcmd -h -E → shows help (preserves '-E' flag)
//         sqlcmd -h 10 → sets header count (unchanged)
//         sqlcmd -help → shows help

Test coverage in cmd/sqlcmd/sqlcmd_test.go:

  • Conversion logic tests (7 cases)
  • End-to-end parsing tests (3 cases)
  • Validates both help behavior and backward compatibility

Verification

✅ All tests pass
sqlcmd -h abc now shows help (instead of "Unknown command" error)
sqlcmd -h -E shows help and preserves -E flag
✅ Backward compatibility maintained: sqlcmd -h 10 still sets header count

Original prompt

This section details on the original issue you should resolve

<issue_title>sqlcmd -? and sqlcmd -h are not working as expected</issue_title>
<issue_description>I am on macOS, zsh shell.

~ % sqlcmd -h
Sqlcmd: '-h': Missing argument. Enter '-?' for help.

~ % sqlcmd -?
zsh: no matches found: -?

sqlcmd --help gives the expected output.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits January 31, 2026 19:52
Co-authored-by: dlevy-msft-sql <194277063+dlevy-msft-sql@users.noreply.github.com>
- Convert -h without an argument to -? to show help
- Keep -h with a numeric argument for setting header count (e.g., -h 10, -h -1)
- Convert -help to --help to show help
- Add comprehensive tests for the new behavior

Co-authored-by: dlevy-msft-sql <194277063+dlevy-msft-sql@users.noreply.github.com>
- Remove trailing whitespace from preprocessHelpFlags function
- Restore NOTICE.md that was accidentally modified by build process

Co-authored-by: dlevy-msft-sql <194277063+dlevy-msft-sql@users.noreply.github.com>
- Remove unnecessary blank lines in convertOsArgs and preprocessHelpFlags
- Add test case for -h followed by non-numeric value

Co-authored-by: dlevy-msft-sql <194277063+dlevy-msft-sql@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix sqlcmd -? and sqlcmd -h functionality issues Fix -h and -help flags to show help instead of requiring arguments Jan 31, 2026
Copilot AI requested a review from dlevy-msft-sql January 31, 2026 20:01
@dlevy-msft-sql dlevy-msft-sql added bug Something isn't working sqlcmd switch switch in existing sqlcmd Size: S Small issue (less than one week effort) needs-work labels Feb 1, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the behavior of the -h and -help flags to show help instead of requiring arguments, addressing issue #604 where sqlcmd -h returned a "Missing argument" error and sqlcmd -? requires quoting in zsh due to glob expansion.

Changes:

  • Added preprocessHelpFlags() function that converts -h to -? when not followed by a number, preserving backward compatibility with -h <number> for setting header count
  • Converts -help to --help to avoid shell parsing issues (since -help would be parsed as -h elp)
  • Added comprehensive test coverage for the new conversion logic and end-to-end parsing behavior

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
cmd/sqlcmd/sqlcmd.go Implements preprocessHelpFlags() function with logic to detect numeric vs non-numeric arguments following -h, and integrates it into the argument preprocessing pipeline
cmd/sqlcmd/sqlcmd_test.go Adds 10 new test cases covering conversion logic (7 tests in TestConvertOsArgs) and end-to-end parsing behavior (3 tests in TestValidCommandLineToArgsConversion)

@dlevy-msft-sql
Copy link
Contributor

@copilot fix the review items

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 <194277063+dlevy-msft-sql@users.noreply.github.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@dlevy-msft-sql dlevy-msft-sql marked this pull request as ready for review February 2, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working in-review Size: S Small issue (less than one week effort) sqlcmd switch switch in existing sqlcmd

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlcmd -? and sqlcmd -h are not working as expected

2 participants