Skip to content

Commit 4256f88

Browse files
authored
Document changing file name when adding @main (#514)
1 parent 9033408 commit 4256f88

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ extension Math.Statistics {
175175

176176
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.
177177

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`.
179+
178180
```swift
179181
@main
180182
struct Math: ParsableCommand {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ struct Count: ParsableCommand {
6363

6464
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.
6565

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.)
6767

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.
6969

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`.
7071
7172
## Working with Named Options
7273

@@ -80,6 +81,7 @@ Counting words in 'readme.md' and writing the result into 'readme.counts'.
8081
We do this by using the `@Option` property wrapper instead of `@Argument`:
8182

8283
```swift
84+
@main
8385
struct Count: ParsableCommand {
8486
@Option var inputFile: String
8587
@Option var outputFile: String
@@ -118,6 +120,7 @@ Counting words in 'readme.md' and writing the result into 'readme.counts'.
118120
Let's change our `Count` type to look like this:
119121

120122
```swift
123+
@main
121124
struct Count: ParsableCommand {
122125
@Option var inputFile: String
123126
@Option var outputFile: String
@@ -155,6 +158,7 @@ Counting words in 'readme.md' and writing the result into 'readme.counts'.
155158
Customize the input names by passing `name` parameters to the `@Option` and `@Flag` initializers:
156159

157160
```swift
161+
@main
158162
struct Count: ParsableCommand {
159163
@Option(name: [.short, .customLong("input")])
160164
var inputFile: String
@@ -189,6 +193,7 @@ OPTIONS:
189193
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:
190194

191195
```swift
196+
@main
192197
struct Count: ParsableCommand {
193198
@Option(name: [.short, .customLong("input")], help: "A file to read.")
194199
var inputFile: String

Sources/ArgumentParser/Documentation.docc/Extensions/AsyncParsableCommand.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22

33
To use `async`/`await` code in your commands' `run()` method implementations, follow these steps:
44

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.
66
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.
88

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+
import Foundation
13+
14+
@main
15+
struct CountLines: AsyncParsableCommand {
16+
@Argument(transform: URL.init(fileURLWithPath:))
17+
var inputFile: URL
18+
19+
mutating func run() async throws {
20+
let fileHandle = try FileHandle(forReadingFrom: inputFile)
21+
let lineCount = try await 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`.
929
1030
### Usage in Swift 5.5
1131

Sources/ArgumentParser/Documentation.docc/Extensions/ParsableCommand.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct Repeat: ParsableCommand {
2020
}
2121
```
2222

23+
> 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`.
24+
2325
## Topics
2426

2527
### Essentials

0 commit comments

Comments
 (0)