Skip to content

Commit c4a089d

Browse files
committed
Add native response file support
Add native response file support in Swift Argument Parser for all `ParsableCommand` and `AsyncParsableCommand` commands. Since the nested response files are supported, also include an experiemental command line argument that provides source location support. Fixes: #846
1 parent e579882 commit c4a089d

25 files changed

Lines changed: 5088 additions & 34 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Add new items at the end of the relevant section under **Unreleased**.
66

77
## [Unreleased]
88

9+
### Additions
10+
11+
- Added source-location tracking for arguments expanded from response files. When at least one `@file` reference is present in the input, parser error messages now include a multi-line `at <file>:<line>` / `included from <file>:<line>` block that pinpoints the offending argument and its full include chain. For pure command-line invocations (no `@file`), error messages are unchanged.
12+
- Added the experimental `--experimental-dump-arguments-source-location` option. When present, the parser fully parses the command line and then prints every parsed argument together with its value and source location (file:line for response-file args, `argv[N]` for command-line args), organized as a subcommand tree. Accepts `=text` (default) or `=json`.
13+
914
---
1015

1116
## [1.8.2] - 2026-06-04

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ OPTIONS:
6868
-h, --help Show help for this command.
6969
```
7070

71+
## Response Files
72+
73+
Swift Argument Parser automatically supports response files, which let you store command-line arguments in text files. This is especially useful for commands with many arguments or for reusable configurations:
74+
75+
```bash
76+
# Store arguments in a file
77+
$ echo "--count 5 --include-counter hello" > args.txt
78+
79+
# Use the response file with @filename syntax
80+
$ repeat @args.txt
81+
1: hello
82+
2: hello
83+
3: hello
84+
4: hello
85+
5: hello
86+
```
87+
88+
Response files support multiple formats, comments, quoted arguments, and can even reference other response files. No code changes are required - this feature works automatically with any `ParsableCommand`. See the [Response Files documentation](https://swiftpackageindex.com/apple/swift-argument-parser/documentation/argumentparser/responsefiles) for complete details.
89+
7190
## Documentation
7291

7392
For guides, articles, and API documentation see the

Sources/ArgumentParser/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ add_library(ArgumentParser
3333
Parsing/Parsed.swift
3434
Parsing/ParsedValues.swift
3535
Parsing/ParserError.swift
36+
Parsing/ResponseFileExpander.swift
3637
Parsing/SplitArguments.swift
3738

3839
Usage/DumpHelpGenerator.swift
3940
Usage/HelpCommand.swift
4041
Usage/HelpGenerator.swift
4142
Usage/MessageInfo.swift
43+
Usage/SourceLocationDumpGenerator.swift
4244
Usage/UsageGenerator.swift
4345

4446
Utilities/CollectionExtensions.swift

Sources/ArgumentParser/Documentation.docc/ArgumentParser.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Begin by declaring a type that defines
1010
the information that you need to collect from the command line.
1111
Decorate each stored property with one of `ArgumentParser`'s property wrappers,
1212
declare conformance to ``ParsableCommand``,
13-
and implement your command's logic in its `run()` method.
13+
and implement your command's logic in its `run()` method.
1414
For `async` renditions of `run`, declare ``AsyncParsableCommand`` conformance instead.
1515

1616
```swift
@@ -33,7 +33,7 @@ struct Repeat: ParsableCommand {
3333
}
3434
```
3535

36-
When a user executes your command,
36+
When a user executes your command,
3737
the `ArgumentParser` library parses the command-line arguments,
3838
instantiates your command type,
3939
and then either calls your `run()` method or exits with a useful message.
@@ -58,6 +58,7 @@ and then either calls your `run()` method or exits with a useful message.
5858
### Arguments, Options, and Flags
5959

6060
- <doc:DeclaringArguments>
61+
- <doc:ResponseFiles>
6162
- ``Argument``
6263
- ``Option``
6364
- ``Flag``

Sources/ArgumentParser/Documentation.docc/Articles/ExperimentalFeatures.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ If you have any feedback on experimental features, please [open a GitHub issue][
1313
| Name | Description | related PRs | Version |
1414
| ------------- | ------------- | ------------- | ------------- |
1515
| `--experimental-dump-help` | Dumps command/argument/help information as JSON | [#310][] [#335][] | 0.5.0 or newer |
16+
| `--experimental-dump-arguments-source-location` | Dumps the parsed argument tree with each value's source location (file:line for response-file args, `argv[N]` for command-line args). Accepts `=text` (default) or `=json`. || 1.9.0 or newer |
1617

1718
[#310]: https://github.com/apple/swift-argument-parser/pull/310
1819
[#335]: https://github.com/apple/swift-argument-parser/pull/335

0 commit comments

Comments
 (0)