Skip to content

Commit

Permalink
Check value of command-line options
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos committed Apr 16, 2024
1 parent 2569d89 commit 4bdf0d8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
35 changes: 29 additions & 6 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4bdf0d8

Please sign in to comment.