Skip to content

Commit 398d6cc

Browse files
Check value of command-line options
1 parent 2569d89 commit 398d6cc

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

codespell_lib/_codespell.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,26 +1238,49 @@ def main(*args: str) -> int:
12381238

12391239
summary = Summary() if options.summary else None
12401240

1241-
context = None
1242-
if options.context is not None:
1243-
if (options.before_context is not None) or (options.after_context is not None):
1241+
if (options.before_context is not None) or (options.after_context is not None):
1242+
if options.context is not None:
12441243
print(
12451244
"ERROR: --context/-C cannot be used together with "
12461245
"--context-before/-B or --context-after/-A",
12471246
file=sys.stderr,
12481247
)
12491248
parser.print_help()
12501249
return EX_USAGE
1251-
context_both = max(0, options.context)
1252-
context = (context_both, context_both)
1253-
elif (options.before_context is not None) or (options.after_context is not None):
1254-
context_before = 0
1255-
context_after = 0
12561250
if options.before_context is not None:
1257-
context_before = max(0, options.before_context)
1251+
if options.before_context < 0:
1252+
print(
1253+
"ERROR: --context-before/-B accepts only positive values",
1254+
file=sys.stderr,
1255+
)
1256+
parser.print_help()
1257+
return EX_USAGE
1258+
context_before = options.before_context
1259+
else:
1260+
context_before = 0
12581261
if options.after_context is not None:
1259-
context_after = max(0, options.after_context)
1262+
if options.after_context < 0:
1263+
print(
1264+
"ERROR: --context-after/-A accepts only positive values",
1265+
file=sys.stderr,
1266+
)
1267+
parser.print_help()
1268+
return EX_USAGE
1269+
context_after = options.after_context
1270+
else:
1271+
context_after = 0
12601272
context = (context_before, context_after)
1273+
elif options.context is not None:
1274+
if options.context < 0:
1275+
print(
1276+
"ERROR: --context/-C accepts positive values only",
1277+
file=sys.stderr,
1278+
)
1279+
parser.print_help()
1280+
return EX_USAGE
1281+
context = (options.context, options.context)
1282+
else:
1283+
context = None
12611284

12621285
exclude_lines: Set[str] = set()
12631286
if options.exclude_file:

codespell_lib/tests/test_basic.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ def test_interactivity(
237237
try:
238238
assert cs.main(fname) == 0, "empty file"
239239
fname.write_text("abandonned\n")
240-
assert cs.main("-i", "-1", fname) == 1, "bad"
241240
with FakeStdin("y\n"):
242241
assert cs.main("-i", "3", fname) == 1
243242
with FakeStdin("n\n"):

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,6 @@ max-complexity = 45
169169
[tool.ruff.lint.pylint]
170170
allow-magic-value-types = ["bytes", "int", "str",]
171171
max-args = 13
172-
max-branches = 51
173-
max-returns = 11
174-
max-statements = 119
172+
max-branches = 55
173+
max-returns = 14
174+
max-statements = 121

0 commit comments

Comments
 (0)