Skip to content

Commit

Permalink
Add --version option to show deadcode version
Browse files Browse the repository at this point in the history
  • Loading branch information
albertas committed Aug 9, 2024
1 parent 3fd16de commit e128c7a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions deadcode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
try:
import importlib.metadata

__version__ = importlib.metadata.version(__package__ or __name__)
except ImportError:
import importlib_metadata

__version__ = importlib_metadata.version(__package__ or __name__)
6 changes: 6 additions & 0 deletions deadcode/actions/parse_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ def parse_arguments(args: Optional[List[str]]) -> Args:
action='store_true',
default=False,
)
parser.add_argument(
'--version',
help='Shows deadcode version',
action='store_true',
default=False,
)

parsed_args = parser.parse_args(args).__dict__

Expand Down
6 changes: 6 additions & 0 deletions deadcode/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List, Optional
import sys

from deadcode import __version__
from deadcode.actions.find_python_filenames import find_python_filenames
from deadcode.actions.find_unused_names import find_unused_names
from deadcode.actions.fix_or_show_unused_code import fix_or_show_unused_code
Expand All @@ -12,6 +14,10 @@
def main(
command_line_args: Optional[List[str]] = None,
) -> Optional[str]:

if command_line_args and '--version' in command_line_args or '--version' in sys.argv:
return __version__

args = parse_arguments(command_line_args)

filenames = find_python_filenames(args=args)
Expand Down
1 change: 1 addition & 0 deletions deadcode/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class Args:
fix: bool = False
verbose: bool = False
version: bool = False
dry: bool = False
only: Iterable[Pathname] = ()
paths: Iterable[Pathname] = ()
Expand Down
41 changes: 41 additions & 0 deletions tests/cli_args/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from deadcode import __version__
from deadcode.cli import main
from deadcode.utils.base_test_case import BaseTestCase


class TestVersionCliOption(BaseTestCase):
def test_show_version(self):
self.files = {
'foo.py': b"""
class UnusedClass:
pass
print("Dont change this file")""",
'bar.py': b"""
def unused_function():
pass
print("Dont change this file")""",
}

result = main('--version'.split())

self.assertEqual(result, __version__)

def test_show_version_when_path_provided(self):
self.files = {
'foo.py': b"""
class UnusedClass:
pass
print("Dont change this file")""",
'bar.py': b"""
def unused_function():
pass
print("Dont change this file")""",
}

result = main('. --version'.split())

self.assertEqual(result, __version__)

0 comments on commit e128c7a

Please sign in to comment.