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 @@ -348,6 +348,20 @@ def _supports_ansi_colors() -> bool:
348
348
return False
349
349
350
350
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
+
351
365
def parse_options (
352
366
args : Sequence [str ],
353
367
) -> Tuple [argparse .Namespace , argparse .ArgumentParser , List [str ]]:
@@ -557,21 +571,21 @@ def parse_options(
557
571
parser .add_argument (
558
572
"-A" ,
559
573
"--after-context" ,
560
- type = int ,
574
+ type = _check_positive_int ,
561
575
metavar = "LINES" ,
562
576
help = "print LINES of trailing context" ,
563
577
)
564
578
parser .add_argument (
565
579
"-B" ,
566
580
"--before-context" ,
567
- type = int ,
581
+ type = _check_positive_int ,
568
582
metavar = "LINES" ,
569
583
help = "print LINES of leading context" ,
570
584
)
571
585
parser .add_argument (
572
586
"-C" ,
573
587
"--context" ,
574
- type = int ,
588
+ type = _check_positive_int ,
575
589
metavar = "LINES" ,
576
590
help = "print LINES of surrounding context" ,
577
591
)
You can’t perform that action at this time.
0 commit comments