Skip to content

Commit 4bdf0d8

Browse files
Check value of command-line options
1 parent 2569d89 commit 4bdf0d8

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

codespell_lib/_codespell.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ def parse_options(
507507
"--interactive",
508508
action="store",
509509
type=int,
510+
choices=(0, 1, 2, 3),
510511
default=0,
511512
help="set interactive mode when writing changes:\n"
512513
"- 0: no interactivity.\n"
@@ -520,6 +521,7 @@ def parse_options(
520521
"--quiet-level",
521522
action="store",
522523
type=int,
524+
choices=range(0, 64),
523525
default=34,
524526
help="bitmask that allows suppressing messages:\n"
525527
"- 0: print all messages.\n"
@@ -1248,15 +1250,36 @@ def main(*args: str) -> int:
12481250
)
12491251
parser.print_help()
12501252
return EX_USAGE
1251-
context_both = max(0, options.context)
1253+
if options.context < 0:
1254+
print(
1255+
"ERROR: --context/-C accepts positive values only",
1256+
file=sys.stderr,
1257+
)
1258+
context_both = options.context
12521259
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
1260+
else:
12561261
if options.before_context is not None:
1257-
context_before = max(0, options.before_context)
1262+
if options.before_context < 0:
1263+
print(
1264+
"ERROR: --context-before/-B accepts only positive values",
1265+
file=sys.stderr,
1266+
)
1267+
parser.print_help()
1268+
return EX_USAGE
1269+
context_before = options.before_context
1270+
else:
1271+
context_before = 0
12581272
if options.after_context is not None:
1259-
context_after = max(0, options.after_context)
1273+
if options.after_context < 0:
1274+
print(
1275+
"ERROR: --context-after/-A accepts only positive values",
1276+
file=sys.stderr,
1277+
)
1278+
parser.print_help()
1279+
return EX_USAGE
1280+
context_after = options.after_context
1281+
else:
1282+
context_after = 0
12601283
context = (context_before, context_after)
12611284

12621285
exclude_lines: Set[str] = set()

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 = 54
173+
max-returns = 13
174+
max-statements = 120

0 commit comments

Comments
 (0)