Skip to content

Commit f798026

Browse files
Check value of command-line options
1 parent faf1c33 commit f798026

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

codespell_lib/_codespell.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ def _supports_ansi_colors() -> bool:
348348
return False
349349

350350

351+
def _check_positive_int(value: str) -> int:
352+
try:
353+
i = int(value)
354+
except ValueError:
355+
# same message as argparse type
356+
message = f"invalid {int.__name__} value: {value!r}"
357+
raise argparse.ArgumentTypeError(message)
358+
if i < 0:
359+
# message similar to argparse choices
360+
message = f"invalid choice: {value} (choose a positive integer)"
361+
raise argparse.ArgumentTypeError(message)
362+
return i
363+
364+
351365
def parse_options(
352366
args: Sequence[str],
353367
) -> Tuple[argparse.Namespace, argparse.ArgumentParser, List[str]]:
@@ -557,21 +571,21 @@ def parse_options(
557571
parser.add_argument(
558572
"-A",
559573
"--after-context",
560-
type=int,
574+
type=_check_positive_int,
561575
metavar="LINES",
562576
help="print LINES of trailing context",
563577
)
564578
parser.add_argument(
565579
"-B",
566580
"--before-context",
567-
type=int,
581+
type=_check_positive_int,
568582
metavar="LINES",
569583
help="print LINES of leading context",
570584
)
571585
parser.add_argument(
572586
"-C",
573587
"--context",
574-
type=int,
588+
type=_check_positive_int,
575589
metavar="LINES",
576590
help="print LINES of surrounding context",
577591
)

0 commit comments

Comments
 (0)