You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/ArgumentParser/Documentation.docc/Articles/CommandsAndSubcommands.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -175,6 +175,8 @@ extension Math.Statistics {
175
175
176
176
Last but not least, we add the `@main` attribute to the root of our command tree, to tell the compiler to use that as the program's entry point. Upon execution, this parses the command-line arguments, determines whether a subcommand was selected, and then instantiates and calls the `run()` method on that particular subcommand.
177
177
178
+
> Note: The Swift compiler uses either the type marked with `@main` or a `main.swift` file as the entry point for an executable program. You can use either one, but not both — rename your `main.swift` file to the name of your command when you add `@main`. In this case, rename it to `Math.swift`.
In the code above, the `inputFile` and `outputFile` properties use the `@Argument` property wrapper. `ArgumentParser` uses this wrapper to denote a positional command-line input — because `inputFile` is specified first in the `Count` type, it's the first value read from the command line, and `outputFile` is the second.
65
65
66
-
We've implemented the command's logic in its `run()` method. Here, we're printing out a message confirming the names of the files the user gave. (You can find a full implementation of the completed command at the end of this guide.)
66
+
The command's logic is implemented in its `run()` method. Here, it prints out a message confirming the names of the files the user gave. (You can find a full implementation of the completed command at the end of this guide.)
67
67
68
-
Finally, you designate the `Count` command as the program's entry point by applying the `@main` attribute. When running your command, the `ArgumentParser` library parses the command-line arguments, verifies that they match up with what we've defined in `Count`, and either calls the `run()` method or exits with a helpful message.
68
+
Finally, the `Count` command is designated as the program's entry point by applying the `@main` attribute. When running your command, the `ArgumentParser` library parses the command-line arguments, verifies that they match up with what we've defined in `Count`, and either calls the `run()` method or exits with a helpful message.
69
69
70
+
> Note: The Swift compiler uses either the type marked with `@main` or a `main.swift` file as the entry point for an executable program. You can use either one, but not both — rename your `main.swift` file to the name of the command when you add `@main`. In this case, rename the file to `Count.swift`.
70
71
71
72
## Working with Named Options
72
73
@@ -80,6 +81,7 @@ Counting words in 'readme.md' and writing the result into 'readme.counts'.
80
81
We do this by using the `@Option` property wrapper instead of `@Argument`:
81
82
82
83
```swift
84
+
@main
83
85
structCount: ParsableCommand {
84
86
@Optionvar inputFile: String
85
87
@Optionvar outputFile: String
@@ -118,6 +120,7 @@ Counting words in 'readme.md' and writing the result into 'readme.counts'.
118
120
Let's change our `Count` type to look like this:
119
121
120
122
```swift
123
+
@main
121
124
structCount: ParsableCommand {
122
125
@Optionvar inputFile: String
123
126
@Optionvar outputFile: String
@@ -155,6 +158,7 @@ Counting words in 'readme.md' and writing the result into 'readme.counts'.
155
158
Customize the input names by passing `name` parameters to the `@Option` and `@Flag` initializers:
156
159
157
160
```swift
161
+
@main
158
162
structCount: ParsableCommand {
159
163
@Option(name: [.short, .customLong("input")])
160
164
var inputFile: String
@@ -189,6 +193,7 @@ OPTIONS:
189
193
This is a great start — you can see that all the custom names are visible, and the help shows that values are expected for the `--input` and `--output` options. However, our custom options and flag don't have any descriptive text. Let's add that now by passing string literals as the `help` parameter:
190
194
191
195
```swift
196
+
@main
192
197
structCount: ParsableCommand {
193
198
@Option(name: [.short, .customLong("input")], help:"A file to read.")
Copy file name to clipboardExpand all lines: Sources/ArgumentParser/Documentation.docc/Extensions/AsyncParsableCommand.md
+22-2Lines changed: 22 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,30 @@
2
2
3
3
To use `async`/`await` code in your commands' `run()` method implementations, follow these steps:
4
4
5
-
1. For the root command in your command-line tool, declare conformance to `AsyncParsableCommand`, even if that command doesn't use asynchronous code.
5
+
1. For the root command in your command-line tool, declare conformance to `AsyncParsableCommand`, whether or not that command uses asynchronous code.
6
6
2. Apply the `@main` attribute to the root command. (Note: If your root command is in a `main.swift` file, rename the file to the name of the command.)
7
-
3. For any command that needs to use asynchronous code, declare conformance to `AsyncParsableCommand` and mark the `run()` method as `async`. No changes are needed for commands that don't use asynchronous code.
7
+
3. For any command that needs to use asynchronous code, declare conformance to `AsyncParsableCommand` and mark the `run()` method as `async`. No changes are needed for subcommands that don't use asynchronous code.
8
8
9
+
The following example declares a `CountLines` command that uses Foundation's asynchronous `FileHandle.AsyncBytes` to read the lines from a file:
10
+
11
+
```swift
12
+
importFoundation
13
+
14
+
@main
15
+
structCountLines: AsyncParsableCommand {
16
+
@Argument(transform: URL.init(fileURLWithPath:))
17
+
var inputFile: URL
18
+
19
+
mutatingfuncrun() asyncthrows {
20
+
let fileHandle =tryFileHandle(forReadingFrom: inputFile)
21
+
let lineCount =tryawait fileHandle.bytes.lines.reduce(into: 0)
22
+
{ count, _in count +=1 }
23
+
print(lineCount)
24
+
}
25
+
}
26
+
```
27
+
28
+
> Note: The Swift compiler uses either the type marked with `@main` or a `main.swift` file as the entry point for an executable program. You can use either one, but not both — rename your `main.swift` file to the name of the command when you add `@main`.
> Note: The Swift compiler uses either the type marked with `@main` or a `main.swift` file as the entry point for an executable program. You can use either one, but not both — rename your `main.swift` file to the name of the command when you add `@main`.
0 commit comments