diff --git a/src/ssort/_main.py b/src/ssort/_main.py index a7efa1d..6e0e58e 100644 --- a/src/ssort/_main.py +++ b/src/ssort/_main.py @@ -3,6 +3,7 @@ import re import sys +from ssort import __version__ from ssort._exceptions import UnknownEncodingError from ssort._files import find_python_files from ssort._ssort import ssort @@ -19,6 +20,12 @@ def main(): description="Sort python statements into dependency order", ) + parser.add_argument( + "--version", + dest="version", + action="store_true", + help="Outputs version information and then exit", + ) parser.add_argument( "--diff", dest="show_diff", @@ -38,6 +45,10 @@ def main(): args = parser.parse_args() + if args.version: + sys.stdout.write(f"ssort {__version__}\n") + return + unsorted = 0 unsortable = 0 unchanged = 0 diff --git a/tests/test_executable.py b/tests/test_executable.py index 0260d26..9d9e0ef 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -4,6 +4,7 @@ import pytest +from ssort import __version__ from ssort._utils import escape_path _good = b""" @@ -393,6 +394,16 @@ def test_ssort_unreadable_file(ssort, tmp_path): assert (actual_msgs, actual_status) == (expected_msgs, expected_status) +def test_ssort_version(): + result = subprocess.run( + ["ssort", "--version"], + capture_output=True, + encoding="utf-8", + ) + output = result.stdout + assert output == f"ssort {__version__}\n" + + def test_ssort_run_module(): entrypoint_result = subprocess.run( ["ssort", "--help"],