Skip to content

Commit e07c535

Browse files
committed
CLI version option
1 parent 6216999 commit e07c535

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

openapi_spec_validator/__main__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from jsonschema.exceptions import ValidationError
77
from jsonschema.exceptions import best_match
88

9+
from openapi_spec_validator import __version__
910
from openapi_spec_validator.readers import read_from_filename
1011
from openapi_spec_validator.readers import read_from_stdin
1112
from openapi_spec_validator.shortcuts import validate
@@ -51,7 +52,7 @@ def print_validationerror(
5152

5253

5354
def main(args: Sequence[str] | None = None) -> None:
54-
parser = ArgumentParser()
55+
parser = ArgumentParser(prog="openapi-spec-validator")
5556
parser.add_argument(
5657
"file",
5758
nargs="+",
@@ -72,6 +73,11 @@ def main(args: Sequence[str] | None = None) -> None:
7273
metavar="{detect,2.0,3.0,3.1}",
7374
help="OpenAPI schema version (default: detect).",
7475
)
76+
parser.add_argument(
77+
"--version",
78+
action="version",
79+
version=f"%(prog)s {__version__}",
80+
)
7581
args_parsed = parser.parse_args(args)
7682

7783
for filename in args_parsed.file:

tests/integration/test_main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

6+
from openapi_spec_validator import __version__
67
from openapi_spec_validator.__main__ import main
78

89

@@ -190,3 +191,13 @@ def test_schema_stdin(capsys):
190191
out, err = capsys.readouterr()
191192
assert not err
192193
assert "stdin: OK\n" in out
194+
195+
196+
def test_version(capsys):
197+
"""Test --version flag outputs correct version."""
198+
testargs = ["--version"]
199+
with pytest.raises(SystemExit):
200+
main(testargs)
201+
out, err = capsys.readouterr()
202+
assert not err
203+
assert out.strip() == f"openapi-spec-validator {__version__}"

0 commit comments

Comments
 (0)