diff --git a/deadcode/actions/parse_arguments.py b/deadcode/actions/parse_arguments.py index b3b36d1..d51ecc3 100644 --- a/deadcode/actions/parse_arguments.py +++ b/deadcode/actions/parse_arguments.py @@ -8,12 +8,11 @@ else: import tomli as tomllib -from deadcode import __version__ from deadcode.data_types import Args from deadcode.utils.flatten_lists import flatten_lists_of_comma_separated_values -def parse_arguments(args: Optional[List[str]]) -> tuple[Args | None, str | None]: +def parse_arguments(args: Optional[List[str]]) -> Args: """Parses arguments (execution options) for deadcode tool. Arguments for DeadCode can be provided via: @@ -204,9 +203,6 @@ def parse_arguments(args: Optional[List[str]]) -> tuple[Args | None, str | None] parsed_args = parser.parse_args(args).__dict__ - if parsed_args['version']: - return None, __version__ - for arg_name, arg_value in parsed_args.items(): if isinstance(arg_value, list) and arg_name != 'paths': parsed_args[arg_name] = flatten_lists_of_comma_separated_values(parsed_args.get(arg_name)) @@ -220,7 +216,7 @@ def parse_arguments(args: Optional[List[str]]) -> tuple[Args | None, str | None] if parsed_args['dry']: parsed_args['fix'] = False - return Args(**parsed_args), None + return Args(**parsed_args) def parse_pyproject_toml() -> Dict[str, Any]: diff --git a/deadcode/cli.py b/deadcode/cli.py index adda988..1741609 100644 --- a/deadcode/cli.py +++ b/deadcode/cli.py @@ -1,5 +1,6 @@ from typing import List, Optional +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 @@ -12,9 +13,11 @@ def main( command_line_args: Optional[List[str]] = None, ) -> Optional[str]: - args, message = parse_arguments(command_line_args) - if not args: - return message + + if command_line_args and '--version' in command_line_args: + return __version__ + + args = parse_arguments(command_line_args) filenames = find_python_filenames(args=args) diff --git a/tests/cli_args/test_parsing.py b/tests/cli_args/test_parsing.py index b921e86..475d558 100644 --- a/tests/cli_args/test_parsing.py +++ b/tests/cli_args/test_parsing.py @@ -21,7 +21,7 @@ def setUp(self): def test_calling_with_one_paths_argument(self): options = '.' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.exclude, []) self.assertEqual(args.ignore_names, []) @@ -29,7 +29,7 @@ def test_calling_with_one_paths_argument(self): def test_calling_with_several_paths_argument(self): options = '. tests' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.', 'tests']) self.assertEqual(args.exclude, []) self.assertEqual(args.ignore_names, []) @@ -37,7 +37,7 @@ def test_calling_with_several_paths_argument(self): def test_calling_with_exclude(self): options = '. --exclude=tests,venv' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.exclude, ['tests', 'venv']) self.assertEqual(args.ignore_names, []) @@ -45,7 +45,7 @@ def test_calling_with_exclude(self): def test_calling_with_several_exclude_options(self): options = '. --exclude=tests,venv --exclude=migrations' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.exclude, ['tests', 'venv', 'migrations']) self.assertEqual(args.ignore_names, []) @@ -53,13 +53,13 @@ def test_calling_with_several_exclude_options(self): def test_calling_with_no_color_option(self): options = '. --no-color' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.no_color, True) def test_ignore_names_and_ignore_files_command_line_argument_parsing(self): options = '. --ignore-names-in-files=tests,venv --ignore-names=BaseTestCase,lambda_handler' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.exclude, []) self.assertEqual(args.ignore_names, ['BaseTestCase', 'lambda_handler']) @@ -67,26 +67,26 @@ def test_ignore_names_and_ignore_files_command_line_argument_parsing(self): def test_verbose_flag_is_on_using_long_name(self): options = '--verbose .' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.verbose, True) self.assertEqual(args.paths, ['.']) def test_verbose_flag_is_on_using_short_name(self): options = '. -v' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.verbose, True) def test_calling_with_fix(self): options = '. --fix' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.dry, False) self.assertEqual(args.fix, True) def test_calling_with_dry(self): options = '. --dry --verbose' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.dry, True) self.assertEqual(args.only, []) @@ -95,14 +95,14 @@ def test_calling_with_dry(self): def test_calling_with_fix_and_dry(self): options = '. --dry --fix' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.dry, True) self.assertEqual(args.fix, False) self.assertEqual(args.only, []) def test_calling_with_dry_and_single_only_filename(self): options = '. --dry --only foo.py --fix' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.dry, True) self.assertEqual(args.only, ['foo.py']) @@ -110,7 +110,7 @@ def test_calling_with_dry_and_single_only_filename(self): def test_calling_with_two_dry_filenames(self): options = '. --fix --dry --only foo.py bar.py' - args, _ = parse_arguments(options.split()) + args = parse_arguments(options.split()) self.assertEqual(args.paths, ['.']) self.assertEqual(args.only, ['foo.py', 'bar.py']) self.assertEqual(args.fix, False) diff --git a/tests/cli_args/test_version.py b/tests/cli_args/test_version.py index 29d51c2..8ade9ab 100644 --- a/tests/cli_args/test_version.py +++ b/tests/cli_args/test_version.py @@ -4,7 +4,25 @@ class TestVersionCliOption(BaseTestCase): - def test_version_shown(self): + 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: