Skip to content

Commit

Permalink
feat(idf.py): Allow adding arguments from file via @filename.txt (#11783
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nebkat committed Jul 6, 2023
1 parent d2471b1 commit 2af4c34
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/en/api-guides/tools/idf-py.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ To enable autocompletion for ``idf.py``, use the ``export`` command (:ref:`Step

The autocomplete support for PowerShell is planned in the future.

Specifying Arguments Via File
-----------------------------

Anywhere on the `idf.py` command line, you can specify a file name as `@filename.txt` to read one or more arguments from text file `filename.txt`. Arguments can be separated by newlines or spaces, quotes can be used to enclose arguments that span multiple words. Arguments read from the text file are expanded exactly as if they had appeared in that order on the `idf.py` command line.

Advanced Commands
=================

Expand Down
29 changes: 27 additions & 2 deletions tools/idf.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ def parse_project_dir(project_dir: str) -> Any:
return CLI(help=cli_help, verbose_output=verbose_output, all_actions=all_actions)


def main() -> None:
def main(argv=None) -> None:
# Check the environment only when idf.py is invoked regularly from command line.
checks_output = None if SHELL_COMPLETE_RUN else check_environment()

Expand All @@ -713,7 +713,32 @@ def main() -> None:
else:
raise
else:
cli(sys.argv[1:], prog_name=PROG, complete_var=SHELL_COMPLETE_VAR)
argv = expand_file_arguments(argv or sys.argv[1:])

cli(argv, prog_name=PROG, complete_var=SHELL_COMPLETE_VAR)


def expand_file_arguments(argv):
"""
Any argument starting with "@" gets replaced with all values read from a text file.
Text file arguments can be split by newline or by space.
Values are added "as-is", as if they were specified in this order
on the command line.
"""
new_args = []
expanded = False
for arg in argv:
if arg.startswith("@"):
expanded = True
with open(arg[1:], "r") as f:
for line in f.readlines():
new_args += shlex.split(line)
else:
new_args.append(arg)
if expanded:
print("idf.py %s" % (" ".join(new_args[1:])))
return new_args
return argv


def _valid_unicode_config() -> Union[codecs.CodecInfo, bool]:
Expand Down

0 comments on commit 2af4c34

Please sign in to comment.