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 398d6cc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
43 changes: 33 additions & 10 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,26 +1238,49 @@ 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",
file=sys.stderr,
)
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:
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 = 55
max-returns = 14
max-statements = 121

0 comments on commit 398d6cc

Please sign in to comment.