Skip to content

Commit 51ec3ed

Browse files
authored
Add AsyncParsableCommand to README (#565)
* See issue #561
1 parent c10af98 commit 51ec3ed

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Begin by declaring a type that defines the information
66
that you need to collect from the command line.
77
Decorate each stored property with one of `ArgumentParser`'s property wrappers,
88
and then declare conformance to `ParsableCommand` and add the `@main` attribute.
9+
(Note, for `async` renditions of `run`, conform to `AsyncParsableCommand` rather
10+
than `ParsableCommand`.)
911
Finally, implement your command's logic in the `run()` method.
1012

1113
```swift
@@ -74,6 +76,7 @@ For guides, articles, and API documentation see the
7476
- [ArgumentParser documentation][docs]
7577
- [Getting Started with ArgumentParser](https://swiftpackageindex.com/apple/swift-argument-parser/documentation/argumentparser/gettingstarted)
7678
- [`ParsableCommand` documentation](https://swiftpackageindex.com/apple/swift-argument-parser/documentation/argumentparser/parsablecommand)
79+
- [`AsyncParsableCommand` documentation](https://swiftpackageindex.com/apple/swift-argument-parser/documentation/argumentparser/asyncparsablecommand)
7780

7881
[docs]: https://swiftpackageindex.com/apple/swift-argument-parser/documentation/argumentparser
7982

Sources/ArgumentParser/Documentation.docc/ArgumentParser.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ 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.
14+
For `async` renditions of `run`, declare ``AsyncParsableCommand`` conformance instead.
1415

1516
```swift
1617
import ArgumentParser

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,31 @@ struct RuntimeError: Error, CustomStringConvertible {
287287
}
288288
}
289289
```
290+
291+
292+
## Next Steps … Swift concurrency
293+
294+
`ArgumentParser` supports Swift concurrency, notably `async` renditions of `run`. If you use `async` rendition of `run`, conform to `AsyncParsableCommand` instead of `ParsableCommand`.
295+
296+
```swift
297+
@main
298+
struct FileUtility: AsyncParsableCommand {
299+
@Argument(
300+
help: "File to be parsed.",
301+
transform: URL.init(fileURLWithPath:)
302+
)
303+
var file: URL
304+
305+
mutating func run() async throws {
306+
let handle = try FileHandle(forReadingFrom: file)
307+
308+
for try await line in handle.bytes.lines {
309+
// do something with each line
310+
}
311+
312+
try handle.close()
313+
}
314+
}
315+
```
316+
317+
> Note: If you accidentally use `ParsableCommand` with an `async` rendition of `run`, the app may never reach your `run` function and may only show the `USAGE` text. If you are using `async` version of `run`, you must use `AsyncParsableCommand`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ While `ArgumentParser` validates that the inputs given by your user match the re
88

99
### Validating Command-Line Input
1010

11-
To validate your commands properties after parsing, implement the ``ParsableArguments/validate()-5r0ge`` method on any ``ParsableCommand`` or ``ParsableArguments`` type. Throwing an error from the `validate()` method causes the program to print a message to standard error and exit with an error code, preventing the `run()` method from being called with invalid inputs.
11+
To validate your commands properties after parsing, implement the ``ParsableArguments/validate()-5r0ge`` method on any ``ParsableCommand``, ``ParsableArguments``, or ``AsyncParsableCommand`` type. Throwing an error from the `validate()` method causes the program to print a message to standard error and exit with an error code, preventing the `run()` method from being called with invalid inputs.
1212

1313
Here's a command that prints out one or more random elements from the list you provide. Its `validate()` method catches three different errors that a user can make and throws a relevant error for each one.
1414

0 commit comments

Comments
 (0)