Skip to content
Open
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
10 changes: 9 additions & 1 deletion sqlparse/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
9 changes: 9 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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