From 398d6cc9f98e96b6478b60deb05d51c14bf9cc91 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:23:03 +0200 Subject: [PATCH] Check value of command-line options --- codespell_lib/_codespell.py | 43 ++++++++++++++++++++++++------- codespell_lib/tests/test_basic.py | 1 - pyproject.toml | 6 ++--- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 62a51b75b34..064fb30e8c2 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -1238,9 +1238,8 @@ def main(*args: str) -> int: summary = Summary() if options.summary else None - context = None - if options.context is not None: - if (options.before_context is not None) or (options.after_context is not None): + if (options.before_context is not None) or (options.after_context is not None): + if options.context is not None: print( "ERROR: --context/-C cannot be used together with " "--context-before/-B or --context-after/-A", @@ -1248,16 +1247,40 @@ def main(*args: str) -> int: ) parser.print_help() return EX_USAGE - context_both = max(0, options.context) - context = (context_both, context_both) - elif (options.before_context is not None) or (options.after_context is not None): - context_before = 0 - context_after = 0 if options.before_context is not None: - context_before = max(0, options.before_context) + if options.before_context < 0: + print( + "ERROR: --context-before/-B accepts only positive values", + file=sys.stderr, + ) + parser.print_help() + return EX_USAGE + context_before = options.before_context + else: + context_before = 0 if options.after_context is not None: - context_after = max(0, options.after_context) + if options.after_context < 0: + print( + "ERROR: --context-after/-A accepts only positive values", + file=sys.stderr, + ) + parser.print_help() + return EX_USAGE + context_after = options.after_context + else: + context_after = 0 context = (context_before, context_after) + elif options.context is not None: + if options.context < 0: + print( + "ERROR: --context/-C accepts positive values only", + file=sys.stderr, + ) + parser.print_help() + return EX_USAGE + context = (options.context, options.context) + else: + context = None exclude_lines: Set[str] = set() if options.exclude_file: diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 51ee4b8390d..6ade3fff6ce 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -237,7 +237,6 @@ def test_interactivity( try: assert cs.main(fname) == 0, "empty file" fname.write_text("abandonned\n") - assert cs.main("-i", "-1", fname) == 1, "bad" with FakeStdin("y\n"): assert cs.main("-i", "3", fname) == 1 with FakeStdin("n\n"): diff --git a/pyproject.toml b/pyproject.toml index 32a566b8134..bb85a67328b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -169,6 +169,6 @@ max-complexity = 45 [tool.ruff.lint.pylint] allow-magic-value-types = ["bytes", "int", "str",] max-args = 13 -max-branches = 51 -max-returns = 11 -max-statements = 119 +max-branches = 55 +max-returns = 14 +max-statements = 121