File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff 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+
232239pre-commit hook
233240---------------
234241
Original file line number Diff line number Diff line change 2323import itertools
2424import os
2525import re
26+ import shlex
2627import sys
2728import textwrap
2829from collections .abc import Iterable , Sequence
@@ -388,7 +389,20 @@ def _supports_ansi_colors() -> bool:
388389def 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+ ) -> None :
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 )
You can’t perform that action at this time.
0 commit comments