Skip to content
Draft
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ instead of these invalid entries:

.. _tomli: https://pypi.org/project/tomli/

Reading arguments from file
---------------------------

Additional arguments can be read from a file with ``@PATH``. Arguments are
extracted using ``shlex.split()``.


pre-commit hook
---------------

Expand Down
14 changes: 13 additions & 1 deletion codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import itertools
import os
import re
import shlex
import sys
import textwrap
from collections.abc import Iterable, Sequence
Expand Down Expand Up @@ -388,7 +389,18 @@ def _supports_ansi_colors() -> bool:
def parse_options(
args: Sequence[str],
) -> tuple[argparse.Namespace, argparse.ArgumentParser, list[str]]:
parser = argparse.ArgumentParser(formatter_class=NewlineHelpFormatter)
# Split lines read from `@PATH` using shlex.split(), otherwise default
# behaviour is to have one arg per line. See:
# https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.convert_arg_line_to_args
class ArgumentParser(argparse.ArgumentParser):
def convert_arg_line_to_args(self, arg_line: str) -> list[str]:
return shlex.split(arg_line)

parser = ArgumentParser(
formatter_class=NewlineHelpFormatter,
fromfile_prefix_chars="@",
epilog="Use @<path> to read additional arguments from file <path>.",
)

parser.set_defaults(colors=_supports_ansi_colors())
parser.add_argument("--version", action="version", version=VERSION)
Expand Down
Loading