Skip to content

Commit 22dc4e0

Browse files
Read additional args from file with with @<path>.
1 parent 0eff305 commit 22dc4e0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ instead of these invalid entries:
229229
230230
.. _tomli: https://pypi.org/project/tomli/
231231

232+
Reading arguments from file
233+
---------------------------
234+
235+
Additional arguments can be read from a file with ``@PATH``. Arguments are
236+
extracted using ``shlex.split()``.
237+
238+
232239
pre-commit hook
233240
---------------
234241

codespell_lib/_codespell.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import itertools
2424
import os
2525
import re
26+
import shlex
2627
import sys
2728
import textwrap
2829
from collections.abc import Iterable, Sequence
@@ -388,7 +389,20 @@ def _supports_ansi_colors() -> bool:
388389
def parse_options(
389390
args: Sequence[str],
390391
) -> tuple[argparse.Namespace, argparse.ArgumentParser, list[str]]:
391-
parser = argparse.ArgumentParser(formatter_class=NewlineHelpFormatter)
392+
393+
# Split lines read from `@PATH` using shlex.split(), otherwise default
394+
# behaviour is to have one arg per line. See:
395+
# https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.convert_arg_line_to_args
396+
class ArgumentParser(argparse.ArgumentParser):
397+
def convert_arg_line_to_args(self, arg_line: str
398+
) -> list[str]:
399+
return shlex.split(arg_line)
400+
401+
parser = ArgumentParser(
402+
formatter_class=NewlineHelpFormatter,
403+
fromfile_prefix_chars="@",
404+
epilog="Use @<path> to read additional arguments from file <path>.",
405+
)
392406

393407
parser.set_defaults(colors=_supports_ansi_colors())
394408
parser.add_argument("--version", action="version", version=VERSION)

0 commit comments

Comments
 (0)