File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -372,6 +372,20 @@ def _supports_ansi_colors() -> bool:
372
372
return False
373
373
374
374
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
+
375
389
def parse_options (
376
390
args : Sequence [str ],
377
391
) -> Tuple [argparse .Namespace , argparse .ArgumentParser , List [str ]]:
@@ -598,21 +612,21 @@ def parse_options(
598
612
parser .add_argument (
599
613
"-A" ,
600
614
"--after-context" ,
601
- type = int ,
615
+ type = _check_positive_int ,
602
616
metavar = "LINES" ,
603
617
help = "print LINES of trailing context" ,
604
618
)
605
619
parser .add_argument (
606
620
"-B" ,
607
621
"--before-context" ,
608
- type = int ,
622
+ type = _check_positive_int ,
609
623
metavar = "LINES" ,
610
624
help = "print LINES of leading context" ,
611
625
)
612
626
parser .add_argument (
613
627
"-C" ,
614
628
"--context" ,
615
- type = int ,
629
+ type = _check_positive_int ,
616
630
metavar = "LINES" ,
617
631
help = "print LINES of surrounding context" ,
618
632
)
You can’t perform that action at this time.
0 commit comments