Skip to content

Commit 7fb35e4

Browse files
committed
Add --version option to show deadcode version
1 parent 3fd16de commit 7fb35e4

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

Diff for: deadcode/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
try:
22
import importlib.metadata
3+
34
__version__ = importlib.metadata.version(__package__ or __name__)
45
except ImportError:
56
import importlib_metadata
7+
68
__version__ = importlib_metadata.version(__package__ or __name__)

Diff for: deadcode/actions/parse_arguments.py

+6
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ def parse_arguments(args: Optional[List[str]]) -> Args:
194194
action='store_true',
195195
default=False,
196196
)
197+
parser.add_argument(
198+
'--version',
199+
help='Shows deadcode version',
200+
action='store_true',
201+
default=False,
202+
)
197203

198204
parsed_args = parser.parse_args(args).__dict__
199205

Diff for: deadcode/cli.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import List, Optional
22

3+
from deadcode import __version__
34
from deadcode.actions.find_python_filenames import find_python_filenames
45
from deadcode.actions.find_unused_names import find_unused_names
56
from deadcode.actions.fix_or_show_unused_code import fix_or_show_unused_code
@@ -12,6 +13,10 @@
1213
def main(
1314
command_line_args: Optional[List[str]] = None,
1415
) -> Optional[str]:
16+
17+
if command_line_args and '--version' in command_line_args:
18+
return __version__
19+
1520
args = parse_arguments(command_line_args)
1621

1722
filenames = find_python_filenames(args=args)

Diff for: deadcode/data_types.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
class Args:
1515
fix: bool = False
1616
verbose: bool = False
17+
version: bool = False
1718
dry: bool = False
1819
only: Iterable[Pathname] = ()
1920
paths: Iterable[Pathname] = ()

Diff for: tests/cli_args/test_version.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from deadcode import __version__
2+
from deadcode.cli import main
3+
from deadcode.utils.base_test_case import BaseTestCase
4+
5+
6+
class TestVersionCliOption(BaseTestCase):
7+
def test_show_version(self):
8+
self.files = {
9+
'foo.py': b"""
10+
class UnusedClass:
11+
pass
12+
13+
print("Dont change this file")""",
14+
'bar.py': b"""
15+
def unused_function():
16+
pass
17+
18+
print("Dont change this file")""",
19+
}
20+
21+
result = main('--version'.split())
22+
23+
self.assertEqual(result, __version__)
24+
25+
def test_show_version_when_path_provided(self):
26+
self.files = {
27+
'foo.py': b"""
28+
class UnusedClass:
29+
pass
30+
31+
print("Dont change this file")""",
32+
'bar.py': b"""
33+
def unused_function():
34+
pass
35+
36+
print("Dont change this file")""",
37+
}
38+
39+
result = main('. --version'.split())
40+
41+
self.assertEqual(result, __version__)

0 commit comments

Comments
 (0)