Skip to content

Commit ff319ab

Browse files
Check value of command-line options
1 parent 9f8ef00 commit ff319ab

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
@@ -372,6 +372,20 @@ def _supports_ansi_colors() -> bool:
372372
return False
373373

374374

375+
def _check_positive_int(value: str) -> int:
376+
try:
377+
i = int(value)
378+
except ValueError:
379+
# same message as argparse type
380+
message = f"invalid {int.__name__} value: {value!r}"
381+
raise argparse.ArgumentTypeError(message)
382+
if i < 0:
383+
# message similar to argparse choices
384+
message = f"invalid choice: {value} (choose a positive integer)"
385+
raise argparse.ArgumentTypeError(message)
386+
return i
387+
388+
375389
def parse_options(
376390
args: Sequence[str],
377391
) -> Tuple[argparse.Namespace, argparse.ArgumentParser, List[str]]:
@@ -598,21 +612,21 @@ def parse_options(
598612
parser.add_argument(
599613
"-A",
600614
"--after-context",
601-
type=int,
615+
type=_check_positive_int,
602616
metavar="LINES",
603617
help="print LINES of trailing context",
604618
)
605619
parser.add_argument(
606620
"-B",
607621
"--before-context",
608-
type=int,
622+
type=_check_positive_int,
609623
metavar="LINES",
610624
help="print LINES of leading context",
611625
)
612626
parser.add_argument(
613627
"-C",
614628
"--context",
615-
type=int,
629+
type=_check_positive_int,
616630
metavar="LINES",
617631
help="print LINES of surrounding context",
618632
)

0 commit comments

Comments
 (0)