Skip to content

Commit cacc0b6

Browse files
committed
Tweak AsyncParsableCommand to support 5.5/5.6
1 parent 7566a6a commit cacc0b6

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

Sources/ArgumentParser/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_library(ArgumentParser
1313
"Parsable Properties/Option.swift"
1414
"Parsable Properties/OptionGroup.swift"
1515

16+
"Parsable Types/AsyncParsableCommand.swift"
1617
"Parsable Types/CommandConfiguration.swift"
1718
"Parsable Types/EnumerableFlag.swift"
1819
"Parsable Types/ExpressibleByArgument.swift"

Sources/ArgumentParser/Parsable Types/AsyncParsableCommand.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,34 @@ public protocol AsyncParsableCommand: ParsableCommand {
1515
}
1616

1717
extension AsyncParsableCommand {
18-
public mutating func run() throws {
19-
throw CleanExit.helpRequest(self)
18+
public static func main() async {
19+
do {
20+
var command = try parseAsRoot()
21+
if var asyncCommand = command as? AsyncParsableCommand {
22+
try await asyncCommand.run()
23+
} else {
24+
try command.run()
25+
}
26+
} catch {
27+
exit(withError: error)
28+
}
2029
}
2130
}
2231

23-
public protocol AsyncMain {
32+
@available(
33+
swift, deprecated: 5.6,
34+
message: "Use @main directly on your root `AsyncParsableCommand` type.")
35+
public protocol AsyncMainProtocol {
2436
associatedtype Command: ParsableCommand
2537
}
2638

27-
extension AsyncMain {
39+
@available(swift, deprecated: 5.6)
40+
extension AsyncMainProtocol {
2841
public static func main() async {
2942
do {
3043
var command = try Command.parseAsRoot()
31-
if var command = command as? AsyncParsableCommand {
32-
try await command.run()
44+
if var asyncCommand = command as? AsyncParsableCommand {
45+
try await asyncCommand.run()
3346
} else {
3447
try command.run()
3548
}
@@ -38,3 +51,4 @@ extension AsyncMain {
3851
}
3952
}
4053
}
54+

Tools/changelog-authors/ChangelogAuthors.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ struct ChangelogAuthors: AsyncParsableCommand {
100100
}
101101
}
102102

103-
@main enum Main: AsyncMain {
104-
typealias Command = ChangelogAuthors
105-
}
103+
#if swift(>=5.6)
104+
@main extension ChangelogAuthors {}
105+
#else
106+
@main struct AsyncMain: AsyncMainProtocol {
107+
typealias Command = ChangelogAuthors
108+
}
109+
#endif

0 commit comments

Comments
 (0)