From 4bdf0d830183fa36ff73fc35c06ef5c88279552c 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 | 35 +++++++++++++++++++++++++------ codespell_lib/tests/test_basic.py | 1 - pyproject.toml | 6 +++--- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 62a51b75b34..74811ec682e 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -507,6 +507,7 @@ def parse_options( "--interactive", action="store", type=int, + choices=(0, 1, 2, 3), default=0, help="set interactive mode when writing changes:\n" "- 0: no interactivity.\n" @@ -520,6 +521,7 @@ def parse_options( "--quiet-level", action="store", type=int, + choices=range(0, 64), default=34, help="bitmask that allows suppressing messages:\n" "- 0: print all messages.\n" @@ -1248,15 +1250,36 @@ def main(*args: str) -> int: ) parser.print_help() return EX_USAGE - context_both = max(0, options.context) + if options.context < 0: + print( + "ERROR: --context/-C accepts positive values only", + file=sys.stderr, + ) + context_both = 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 + else: 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) exclude_lines: Set[str] = set() 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..1956d38662a 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 = 54 +max-returns = 13 +max-statements = 120