From 5d7be279c9eb2b3a723809ff7b0fae5d38df8eb2 Mon Sep 17 00:00:00 2001 From: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com> Date: Sat, 25 Jul 2026 14:25:45 -0700 Subject: [PATCH] fix(cli): point at --help when arguments are invalid Running `sqlformat` with no arguments (or any invalid argument combination handled by argparse) printed only the usage line and the error, giving no hint that `--help` exists. argparse's default `error()` is overridden in a small ArgumentParser subclass so every argument error is followed by "Try 'sqlformat --help' for more information." Exit code stays 2. Closes #840 --- sqlparse/cli.py | 10 +++++++++- tests/test_cli.py | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/sqlparse/cli.py b/sqlparse/cli.py index 03ee86e3..caaf647a 100644 --- a/sqlparse/cli.py +++ b/sqlparse/cli.py @@ -27,10 +27,18 @@ # TODO: Add CLI Tests # TODO: Simplify formatter by using argparse `type` arguments +class _ArgumentParser(argparse.ArgumentParser): + """ArgumentParser that points at --help when arguments are invalid.""" + + def error(self, message): + self.exit(2, f'{self.format_usage()}{self.prog}: error: {message}\n' + f"Try '{self.prog} --help' for more information.\n") + + def create_parser(): _CASE_CHOICES = ['upper', 'lower', 'capitalize'] - parser = argparse.ArgumentParser( + parser = _ArgumentParser( prog='sqlformat', description='Format FILE according to OPTIONS. Use "-" as FILE ' 'to read from stdin.', diff --git a/tests/test_cli.py b/tests/test_cli.py index 4aec44d3..a01f0e7b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -204,3 +204,12 @@ def test_cli_error_handling_continues(tmpdir, capsys): assert "select * from baz" in file3.read() _, err = capsys.readouterr() assert "Failed to read" in err + + +def test_cli_argument_error_mentions_help(capsys): + """Argument errors should point the user at --help.""" + with pytest.raises(SystemExit) as exinfo: + sqlparse.cli.main([]) + assert exinfo.value.code == 2 + _, err = capsys.readouterr() + assert "Try 'sqlformat --help' for more information." in err