From 83f3cb6a8e09cab730bc20f698d07c142b684027 Mon Sep 17 00:00:00 2001 From: "C.A.M. Gerlach" Date: Tue, 19 Apr 2022 02:03:36 -0500 Subject: [PATCH] Add version option to QtPy CLI --- qtpy/cli.py | 11 +++++++++++ qtpy/tests/test_cli.py | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/qtpy/cli.py b/qtpy/cli.py index ae0a855a..89ebaf78 100644 --- a/qtpy/cli.py +++ b/qtpy/cli.py @@ -13,6 +13,12 @@ import textwrap +def print_version(): + """Print the current version of the package.""" + import qtpy + print('QtPy version', qtpy.__version__) + + def generate_mypy_args(): """Generate a string with always-true/false args to pass to mypy.""" options = {False: '--always-false', True: '--always-true'} @@ -38,6 +44,11 @@ def generate_arg_parser(): description='Features to support development with QtPy.', ) parser.set_defaults(func=parser.print_help) + + parser.add_argument( + '--version', action='store_const', dest='func', const=print_version, + help='If passed, will print the version and exit') + cli_subparsers = parser.add_subparsers( title='Subcommands', help='Subcommand to run', metavar='Subcommand') diff --git a/qtpy/tests/test_cli.py b/qtpy/tests/test_cli.py index 5abff746..42a10f33 100644 --- a/qtpy/tests/test_cli.py +++ b/qtpy/tests/test_cli.py @@ -25,6 +25,16 @@ def test_cli_help_does_not_fail(subcommand): ) +def test_cli_version(): + output = subprocess.run( + [sys.executable, '-m', 'qtpy', '--version'], + capture_output=True, + check=True, + encoding='utf-8', + ) + assert output.stdout.strip().split()[-1] == qtpy.__version__ + + def test_cli_mypy_args(): output = subprocess.run( [sys.executable, '-m', 'qtpy', 'mypy-args'],