Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check value of command-line options #3408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,20 @@
return False


def _check_positive_int(value: str) -> int:
try:
i = int(value)
except ValueError:

Check warning on line 378 in codespell_lib/_codespell.py

View check run for this annotation

Codecov / codecov/patch

codespell_lib/_codespell.py#L378

Added line #L378 was not covered by tests
# same message as argparse type
message = f"invalid {int.__name__} value: {value!r}"
raise argparse.ArgumentTypeError(message)

Check warning on line 381 in codespell_lib/_codespell.py

View check run for this annotation

Codecov / codecov/patch

codespell_lib/_codespell.py#L380-L381

Added lines #L380 - L381 were not covered by tests
if i < 0:
# message similar to argparse choices
message = f"invalid choice: {value} (choose a positive integer)"
raise argparse.ArgumentTypeError(message)

Check warning on line 385 in codespell_lib/_codespell.py

View check run for this annotation

Codecov / codecov/patch

codespell_lib/_codespell.py#L384-L385

Added lines #L384 - L385 were not covered by tests
return i


def parse_options(
args: Sequence[str],
) -> Tuple[argparse.Namespace, argparse.ArgumentParser, List[str]]:
Expand Down Expand Up @@ -598,21 +612,21 @@
parser.add_argument(
"-A",
"--after-context",
type=int,
type=_check_positive_int,
metavar="LINES",
help="print LINES of trailing context",
)
parser.add_argument(
"-B",
"--before-context",
type=int,
type=_check_positive_int,
metavar="LINES",
help="print LINES of leading context",
)
parser.add_argument(
"-C",
"--context",
type=int,
type=_check_positive_int,
metavar="LINES",
help="print LINES of surrounding context",
)
Expand Down
Loading